446,332 Members | 1,404 Online 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
19 Replies

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

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

 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

 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

 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

 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

 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

 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 int a = 5; int b = 7;   int temp = a; a = b; b = a;   Jun 6 '07 #10

 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

 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

 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

 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

 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

 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

 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 int a = 2147483640; int b = 100;   a = a+b;  //should overflow   Please correct me if I'm wrong. Jun 6 '07 #17

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

 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 int a = 2147483640; int b = 100;   a = a+b;  //should overflow   Please correct me if I'm wrong. Your wrong. ;) Try this: Expand|Select|Wrap|Line Numbers #include  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 = a-b;   cout << endl << "b = a-b" << endl;   cout << "a = " << a << endl;   cout << "b = " << b << endl;   a = a-b;   cout << endl << "a = b-a" << 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 vice-versa. This probably can be explained using modulus arithmetic, but I'm not going there right now. :D Adrian Jun 6 '07 #19

 Expert 100+ P: 844 Your wrong. ;) Try this: Expand|Select|Wrap|Line Numbers #include  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 = a-b;   cout << endl << "b = a-b" << endl;   cout << "a = " << a << endl;   cout << "b = " << b << endl;   a = a-b;   cout << endl << "a = b-a" << 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 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 