This is a more "human" way of solving the problem...
By yourself you would just say:
Well, A=123 and B=456, so swapped its A=456 and B=123, wheres the problem...
However, if you implement your solution in a computer program, it would be something like
a := a + b - a; { old pascal style... }
b := a + b - b;
because you dont want to store a+b in an extra variable.
If the computer computes this with a=123 and b=456 he will end up with:
a := 123 + 456 - 123 = 456;
after this is a = 456!
b := 456 + 456 - 456 = 456;
and the swapping failed...
Dont forget, a cpu itself cant remember anything, its dumb as a brick (of copper/aluminium/etc...)
If you want to realize the problem the way it occurs to the computer, just increase the amount of data insanely.
Please swap an array of... lets say... 2 mio integers by looking on it...
I hope this helped you |