P: 2

Write a macros in which swap two number without using 3rd variable and then call
Macro in main function.
Hint: a=4, b=2 after swapping a=2, b=4.
 
Share this Question
Expert Mod 5K+
P: 9,197

Yes, what to do is clear.
What is your question?
  Expert Mod 5K+
P: 9,197

Moving this to C/C++ forum
  Expert 100+
P: 1,764

Write a macros in which swap two number without using 3rd variable and then call
Macro in main function.
Hint: a=4, b=2 after swapping a=2, b=4.
Here are some hints:
1.Make variable a to have value of a+b
2.Make variable b to have a value of ab.
There is only one more step left to swap values,but I will leave it to u.
It's up to u to figure out last step,I think that's fair.
Savage
  Expert 100+
P: 1,251

Here are some hints:
1.Make variable a to have value of a+b
2.Make variable b to have a value of ab.
There is only one more step left to swap values,but I will leave it to u.
It's up to u to figure out last step,I think that's fair.
Savage
That is interesting, I never thought of doing it that way. I would have used xor.
Adrian
  Expert 100+
P: 844

That is interesting, I never thought of doing it that way. I would have used xor.
Adrian
Yea but I still would use the one with the temp variable if was going to depend on it. The xor would fail if you are trying to swap two same numbers and Savage's way could cause an overflow.
  Expert 100+
P: 1,251

Yea but I still would use the one with the temp variable if was going to depend on it. The xor would fail if you are trying to swap two same numbers and Savage's way could cause an overflow.
I agree with the overflow (I thought something was wrong using addition and subtraction) but xor will not fail even if it is the same number that is being swapped.
Adrian
  Expert 100+
P: 1,764

I agree with the overflow (I thought something was wrong using addition and subtraction) but xor will not fail even if it is the same number that is being swapped.
Adrian
Niether my method would fail if the numbers are equal..
Savage
 
P: 93

Yea but I still would use the one with the temp variable if was going to depend on it. The xor would fail if you are trying to swap two same numbers and Savage's way could cause an overflow.
Why do you need third cariable as Savage has already given you the answer.
Lets assume a = 2 and b = 8
start
1. a = a + b // a will have 10 now
2. b = a  b // b will have (10  8) that's 2.
//a value 10 and b value 2
3. a = a  b // a will have (10  2) that's 8
// thus now a value is 8 and b is 2
Regards,
Girish.
  Expert 100+
P: 844

Why do you need third cariable as Savage has already given you the answer.
Lets assume a = 2 and b = 8
start
1. a = a + b // a will have 10 now
2. b = a  b // b will have (10  8) that's 2.
//a value 10 and b value 2
3. a = a  b // a will have (10  2) that's 8
// thus now a value is 8 and b is 2
Regards,
Girish.
I understand, but the normal way of doing it is with a temporary variable: 
int a = 5;

int b = 7;


int temp = a;

a = b;

b = a;

  Expert 100+
P: 844

I agree with the overflow (I thought something was wrong using addition and subtraction) but xor will not fail even if it is the same number that is being swapped.
Adrian
Yea you're correct Adrian, I thought I read something about it once, but it was only if the two are pointers and they point to the same location and they have the same value. I misread it thinking that if it could only if the value was the same but not
  100+
P: 284

Yea but I still would use the one with the temp variable if was going to depend on it. The xor would fail if you are trying to swap two same numbers and Savage's way could cause an overflow.
May I ask why his method would cause an overflow?
blaze
  Expert 100+
P: 844

May I ask why his method would cause an overflow?
blaze
If a and b are both very large numbers, when you add them together, the sum may exceed INT_MAX (or whatever) and will loop back to INT_MIN (or whatever) which will ruin your data.
  Expert 100+
P: 1,764

If a and b are both very large numbers, when you add them together, the sum may exceed INT_MAX (or whatever) and will loop back to INT_MIN (or whatever) which will ruin your data.
I have tested it,and it doesn't overflow.
Both a and b are integers:
here is input:
a=150000
b=570000
and the output was correct.
Savage
  Expert 100+
P: 1,251

I have tested it,and it doesn't overflow.
Both a and b are integers:
here is input:
a=150000
b=570000
and the output was correct.
Savage
Yeah, your right. The rollover is bidirectional. For some reason, I thought of the carry as holding something useful. I was wrong. Interesting though.
Adrian
  Expert 100+
P: 1,764

Yeah, your right. The rollover is bidirectional. For some reason, I thought of the carry as holding something useful. I was wrong. Interesting though.
Adrian
I know,I thinked some time ago same as u,but when I tested it.....
Savage
  Expert 100+
P: 844

I have tested it,and it doesn't overflow.
Both a and b are integers:
here is input:
a=150000
b=570000
and the output was correct.
Savage
Huh?, I don't understand. The sum of those two is 720000. I'm assuming your INT_MAX is 2147483647 because if it was 32767 your original integers should overflow. 720000 doesn't exceed 2147483647. Did you try with bigger numbers: 
int a = 2147483640;

int b = 100;


a = a+b; //should overflow

Please correct me if I'm wrong.
  Expert 100+
P: 1,764

Huh?, I don't understand. The sum of those two is 720000. I'm assuming your INT_MAX is 2147483647 because if it was 32767 your original integers should overflow. 720000 doesn't exceed 2147483647. Did you try with bigger numbers: 
int a = 2147483640;

int b = 100;


a = a+b; //should overflow

Please correct me if I'm wrong.
Nope,it doesn't overflow on those numbers.And it wasnt long,it was just a short int
Savage
  Expert 100+
P: 1,251

Huh?, I don't understand. The sum of those two is 720000. I'm assuming your INT_MAX is 2147483647 because if it was 32767 your original integers should overflow. 720000 doesn't exceed 2147483647. Did you try with bigger numbers: 
int a = 2147483640;

int b = 100;


a = a+b; //should overflow

Please correct me if I'm wrong.
Your wrong. ;)
Try this:  #include <iostream>

using namespace std;



void swapTest(int a, int b)

{

cout << "" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

a = a+b; //should overflow

cout << endl << "a = a+b" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

b = ab;

cout << endl << "b = ab" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

a = ab;

cout << endl << "a = ba" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

}



int main()

{

swapTest(2147483640, 100);

swapTest(30, 2);

}

What happens is that when you exceed the max value, it wraps to the min value and viceversa. This probably can be explained using modulus arithmetic, but I'm not going there right now. :D
Adrian
  Expert 100+
P: 844

Your wrong. ;)
Try this:  #include <iostream>

using namespace std;



void swapTest(int a, int b)

{

cout << "" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

a = a+b; //should overflow

cout << endl << "a = a+b" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

b = ab;

cout << endl << "b = ab" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

a = ab;

cout << endl << "a = ba" << endl;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

}



int main()

{

swapTest(2147483640, 100);

swapTest(30, 2);

}

What happens is that when you exceed the max value, it wraps to the min value and viceversa. This probably can be explained using modulus arithmetic, but I'm not going there right now. :D
Adrian
Ah now I understand, it does overflow but it overflows in the other direction as well, so everything works out fine. Thanks.
    Question stats  viewed: 1991
 replies: 19
 date asked: Jun 5 '07
