By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Macro in main function.

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.
Jun 5 '07 #1
Share this Question
Share on Google+
19 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, what to do is clear.

What is your question?
Jun 5 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Moving this to C/C++ forum
Jun 5 '07 #3

Savage
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 a-b.

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
Jun 5 '07 #4

AdrianH
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 a-b.

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
Jun 6 '07 #5

ilikepython
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.
Jun 6 '07 #6

AdrianH
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
Jun 6 '07 #7

Savage
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
Jun 6 '07 #8

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.
Jun 6 '07 #9

ilikepython
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:
Expand|Select|Wrap|Line Numbers
  1. int a = 5;
  2. int b = 7;
  3.  
  4. int temp = a;
  5. a = b;
  6. b = a;
  7.  
Jun 6 '07 #10

ilikepython
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
Jun 6 '07 #11

blazedaces
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
Jun 6 '07 #12

ilikepython
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.
Jun 6 '07 #13

Savage
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
Jun 6 '07 #14

AdrianH
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
Jun 6 '07 #15

Savage
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
Jun 6 '07 #16

ilikepython
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:

Expand|Select|Wrap|Line Numbers
  1. int a = 2147483640;
  2. int b = 100;
  3.  
  4. a = a+b;  //should overflow
  5.  
Please correct me if I'm wrong.
Jun 6 '07 #17

Savage
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:

Expand|Select|Wrap|Line Numbers
  1. int a = 2147483640;
  2. int b = 100;
  3.  
  4. a = a+b;  //should overflow
  5.  
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
Jun 6 '07 #18

AdrianH
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:

Expand|Select|Wrap|Line Numbers
  1. int a = 2147483640;
  2. int b = 100;
  3.  
  4. a = a+b;  //should overflow
  5.  
Please correct me if I'm wrong.
Your wrong. ;)

Try this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void swapTest(int a, int b)
  6. {
  7.   cout << "-------------------" << endl;
  8.   cout << "a = " << a << endl;
  9.   cout << "b = " << b << endl;
  10.   a = a+b;  //should overflow
  11.   cout << endl << "a = a+b" << endl;
  12.   cout << "a = " << a << endl;
  13.   cout << "b = " << b << endl;
  14.   b = a-b;
  15.   cout << endl << "b = a-b" << endl;
  16.   cout << "a = " << a << endl;
  17.   cout << "b = " << b << endl;
  18.   a = a-b;
  19.   cout << endl << "a = b-a" << endl;
  20.   cout << "a = " << a << endl;
  21.   cout << "b = " << b << endl;
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27.   swapTest(2147483640, 100);
  28.   swapTest(30, 2);
  29. }
  30.  
What happens is that when you exceed the max value, it wraps to the min value and vice-versa. This probably can be explained using modulus arithmetic, but I'm not going there right now. :D

Adrian
Jun 6 '07 #19

ilikepython
Expert 100+
P: 844
Your wrong. ;)

Try this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void swapTest(int a, int b)
  6. {
  7.   cout << "-------------------" << endl;
  8.   cout << "a = " << a << endl;
  9.   cout << "b = " << b << endl;
  10.   a = a+b;  //should overflow
  11.   cout << endl << "a = a+b" << endl;
  12.   cout << "a = " << a << endl;
  13.   cout << "b = " << b << endl;
  14.   b = a-b;
  15.   cout << endl << "b = a-b" << endl;
  16.   cout << "a = " << a << endl;
  17.   cout << "b = " << b << endl;
  18.   a = a-b;
  19.   cout << endl << "a = b-a" << endl;
  20.   cout << "a = " << a << endl;
  21.   cout << "b = " << b << endl;
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27.   swapTest(2147483640, 100);
  28.   swapTest(30, 2);
  29. }
  30.  
What happens is that when you exceed the max value, it wraps to the min value and vice-versa. 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.
Jun 6 '07 #20

Post your reply

Sign in to post your reply or Sign up for a free account.