Programming

I need to swap two values in the same array in c++. The professor gave us the beginning of the code (pic related). I'm having trouble understanding what to do here. I keep getting the same error. Any help is appreciated.

Attached: hw422.jpg (1920x871, 181K)

Why don't you ask your professor for help rather than Jow Forums?

My professor won't help.
Also, even if he tried to help, he is an awful professor and I have been basically teaching myself this entire class anyway.

bump

Your function declaration is fucked up where you define swap()

Change that to swap(int *, int *) instead of naming it what array you want. That might not be your only issue but it's definitely the one giving you the compiling error.

pic related

Attached: opsproblem.png (1920x871, 879K)

Don't learn to code

>lab14
>still can't even do a function definition right

Is there a programming equivalent to pic related?

Attached: dekinai.jpg (400x450, 39K)

Here my C code (C++ is shit).
void swapany(size_t sz, void *_d1, void *_d2)
{
char *d[2];
size_t i;
d[0] = _d1;
d[1] = _d2;
for (i = 0U; i < sz; i++) {
d[0][i] ^= d[1][i];
d[1][i] ^= d[0][i];
d[0][i] ^= d[1][i];
}
}

void swap(int *n1, int *n2)
{
swapany(sizeof(*n1), n1, n2);
}

Thanks that's all I needed to figure out. This professor is awful at teaching us how to code. Got it now.

Also, this teacher isn't going to give a shit about whether I ask the internet for help or not.

And finally thanks for the compliment I appreciate it.

Y'all have been great. Mission accomplished.

Attached: completehw.jpg (961x823, 73K)

Op do you know what a pointer is?
A pointer can point to an array element.
Your function should work on pointers, generically. Not array elements.
Your function should take 2 pointers. Your function should swap the pointers.

Even better.
void swapchar(char *c1, char *c2)
{
*c1 ^= *c2;
*c2 ^= *c1;
*c1 ^= *c2;
}

void swapany(size_t sz, void *_d1, void *_d2)
{
char *d1;
char *d2;
size_t i;
d1 = _d1;
d2 = _d2;
for (i = 0U; i < sz; i++) {
swapchar(&d1[i], &d2[i]);
}
}

void swap(int *n1, int *n2)
{
swapany(sizeof(*n1), n1, n2);
}

...

>
If that gave you that output, its by pure luck.

Are you retarded? Grab two pointers
A ^=*B ^=*A ^=*B

Not sure how it would be by luck. The logic seems fine.

Does shit like this really count as higher education?
This is only slightly above brainlet tier if you want to do it without a temporary buffer (hint: XOR). I recently talked to someone with a Master's degree in CS and he didn't know what a total language is or what it means for one to be Turing complete.

And in this godforsaken country they won't even give me the certification I need to entroll in the first place. Nuclear Armageddon can't come soon enough.

fucking lmao

please actually turn this in

>Turing complete
>total language
Kek you think anyone needs to know this ultranerd shit when they make more money throwing javascript feces everywhere?
This is like saying wallmart cashiers need to understand the axiom of choice to operate cash registers.

How does that even work?

"Wow you are a beginning programmer? how stupid of you to not be born with intricate understandings of how code works and what all different code languages are. Even people with masters degrees are completely inferior to my mind. How dare universities not accept my 6th grade promotion certificate as qualification to enroll in their computer science program. Everyone should just die already because they can't stand up to my super inteligent brain power."

That's how you sound.
Let that sink in.

OP here.
I just put the value of list[0] into another location temporarily. Then I set list[0] to equal list[4] and list[4] to equal the value in the temporary location.

What in the name of sneed!

When I was tutoring freshmen, I had a girl ask me what if-then-else is about two months in.

Have you tried recompiling it with different input values and swapping different array indexes, my negro friend?

I almost died

What makes you think your not already dead?

How does it even compile?

Doesn't he define a new list within swap, alter that uninitialized list and end up with the correct result?

Who in this thread knows the difference between pointers and references in C++. Just curious.

I do

Logic is fine, but the execution is... I don't know how to describe it...

First off the the 2nd list array is in a different scope than the first one, so the 2nd list is basically an array of pointers. But how the fuck does it still give correct output is beyond me.

References are basically read only / constant pointers, right?

references cannot be lvalues

I know what it sounds like. Yeah, I'm angry, what are you gonna do about it? If OP didn't want to get ridiculed for this shit, he should've tried doing his own research, which would have been faster and given him more in-depth answers, too.

By the way, I also provided some useful direction by hinting at a non-naive approach, so it's not like my post was nothing but bitching and whining. So fuck you, too! Have a nice day! Oh, and before you go that route, I'm not a virgin, thank you very much.

Attached: 1553792158492.gif (720x480, 126K)

Seriously, how does that even work?
Can someone explain to me, what's up?
The desired list variable is out of scope. Why does it change the array?
He passes int s as pointers, no warnings...
What's happening? Am I a complete idiot?
I even checked it out myself, I thought maybe clang is shit, apparently gcc is doing the same...

Swap is using a deprecated way to pass arguments in C/C++, so list[0] -> *temp and list[4] -> *list[size]. C is weakly typed, so the compiler doesn't have to care if what you pass it makes sense, it just takes as many bytes as it needs and carries on. Some of the other behavior is probably a mixture of UB and reading/writing to uninitialized memory and luck.

His code is actually calling the swap method already defined in std, not the method that he has written. If you've copied his code try putting a break point in the swap method, it won't get hit.

jesus christ fucking lmao how does that code even compile
void swap(int* a, int* b) {
const int c = *a;
*a = *b;
*b = c;
}

int main() {
int list[6] = {1,2,3,4,5,6};
swap(&list[0], &list[4]);
return 0;
}

what in the absolute fuck is this hahahahaha

lmfao you dumb nigger, your "using namespace std" will cause std::swap to get called, not that abomination that you wrote

Now say that without crying

cope

These fail if both pointers are to the same location. The first XOR will clear the value to 0.

OP doxxed himself LOL

it just werks

Delta College in Michigan? That's like an hour from me you wanna meet up OP?

i think he wants you to visit him

Someone find OP's professor and email him

The implicit assumption is that these are array elements, Dumbo McBumbo

void swapVals(int i, int j, int* arr[]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

do you have any idea how many of my classmates have basically zero understanding of a pointer because they all use java

You're passing a pointer to the function not modifying the array as it seems use & to actually change the values of function args

Could you explain how that works? I've never seen that operator

That's XOR, "exclusive or". Means 1 if A or B but not both.
So let
A = 01011011
B = 01011110
Now take A^B and put into C
C = 00000101
Since C holds the parity of A,B. As long as you have C and either a or b you can get the other (b or a).

So if we make A = B, overwriting A. And Xor A and C, we can get back our previous A and put it into B
A=B=01011110
__ C=00000101
A^C=01011011
Which is our original A, essentially swapping A and B

>It's another newfaggot-does-C++-without-C++-features episode

Attached: reeeee.jpg (600x600, 20K)