Hi friends
I need an algorithm to swap two variables without using temporary variables,arithmatic operators and pointers. Please tell your logic to solve this problem.
Hi friends
I need an algorithm to swap two variables without using temporary variables,arithmatic operators and pointers. Please tell your logic to solve this problem.
hello
#include <iostream>
using namespace std;
int main()
{
float a = 1.7;
float b = -7.1;
cout << "a = " << a << " b = " << b << endl;
// swap a with b
a = a + b;
b = a - b;
a = a - b;
cout << "after swapping a with b:" << endl;
cout << "a = " << a << " b = " << b << endl;
cin.get(); // wait
return EXIT_SUCCESS;
}
this is the logic of simple program
bye
hello
the program given will help u to decide the algorithem okey
so BUILD IT ON UR OWN very easy . try it out im sure u will get
bye[img]smileys/smiley1.gif[/img]
Hey use these lines intead of those given by sowmya
think ^ denotes XOR
a = a ^ b
b = b ^ a
a = a ^ b
you can decode these steps as
1. a = a ^ b -> a has both the values of a and b.
2. b = b ^ a -> this is equivalent to b = b ^ (a ^ b). XORing same values results in 1. So the value a is set to b.
3. a = a ^ b -> this is equivalent to a = (a ^ b) ^ (a). So the value b is set to a.
Note : the values betwwen ( and ) gives the value replaced for that variable.
Thanks Sankar... I was really searching for this logic.
Great job dude.......
Rock on..............
[img]smileys/smiley1.gif[/img][img]smileys/smiley17.gif[/img][img]smileys/smiley2.gif[/img]
ArunabhA
******************
*---------------------*
* LET THERE BE LIGHT *
*---------------------*
******************
hello
Ya this is the logic XOR operator will do it .. Gud work Sarkar!
bye
sowmya
There are currently 1 users browsing this thread. (0 members and 1 guests)