This seemingly small code which swapps 2 numbers is giving me a headache..help!! C++
+ am wondering why would one need to swapp the numbers in real life? trickery? magic

Please show the code. Without that, it isn't possible to answer your question. The typical swap algorithm is this

swap(type& var1, type& var2)
{
    type tmp = var1;
    var1 = var2;
    var2 = tmp;
}

am wondering why would one need to swapp the numbers in real life?

One very good reason is sorting numbers in either ascending or descending sequence.

Are you talking about the xor-swap trick? Like this:

void swap(int& x, int& y)
  x ^= y;
  y ^= x;
  x ^= y;
};
commented: Elegant. The question is whether 3 xors is more efficient than one stack operation and 3 assignments... Probably! :-) +0

I got turned down for a job once for giving that as a way to swap two values without using a temp value. Too much of a liking for "elegant cleverness", apparently.

y would one need to swapp the numbers in real life? trickery? magic

do you mean applications of swapping ? if yes , agree with AD.
its used in sorting process.

lets suppose there are 3 values {3,2,1} at 3 location x,y,z respectively.And to sort these values in ascending order therefore we need to perform swapping as follow:

if 3 is bigger than 2 then swap the content of loc x and y
if 3 is bigger than 1 then swap the content of loc x and z and so on....

Interesting that the poster of this thread is using the handle of one of the inventors of the C programming language... :-)

Why post above mine get -1 vote. It's perfectly fine solution.

int a = 10;
int b = 20;
a = a+b;
b = a-b;
a = a-b;

// or same with -> b=a+b-(a=b)

cout << a << endl;
cout << b << endl;

print out:
20
10

It wasn't me who down-voted that post. But I can understand why: (1) just one line of code with no explanation is not very helpful, (2) the code is indeed wrong (undefined behavior), and (3) the corrected version of that code (Kristian_2's version) is still problematic due to overflow problems.

The reason why b = a + b - (a = b); is wrong is because of the order of evaluation of the parameters. The first "a" appearance (left-to-right) in that statement could just as well evaluate to the original value of "a", as it could evaluate to the new value of "a" (which is "b"). The code works if you assume that the first "a" appearance is evaluated to the original value of "a". But there is no guarantee that this will be the case, i.e., it is undefined behavior (UB). It's basically the same problem as with (++i + i++) which could evaluate to just about anything.

Also, even if it wasn't undefined behavior, it is still a swap method that requires additional storage, because a temporary variable must be created (by the compiler) to store the original value of "a" while it is being assigned a new value, and so, the equivalent code would be int t = a; a = b; b = t;, which is the traditional swapping method using a temporary variable.

And the reason why Kristian_2's version is also problematic is because it cannot work for all numbers, due to overflow. If both a and b are very large, then a+b could overflow, leading to undefined behavior. You would have similar (or worse) problems with unsigned types. With floating-point numbers (float or double), the problem is more subtle because in that case, overflow is not the problem but rather if there is a great mismatch in the magnitude of the two numbers, the accuracy will be greatly reduced, or even lost completely. In other words, for floating-point numbers, that method is not a perfect swap, it degrades or destroys the values.

Thank you mike_2000_17 for pointing that out. Really nice explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.