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.
19 2394
Yes, what to do is clear.
What is your question?
Moving this to C/C++ forum
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
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
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.
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
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
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.
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;
-
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
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
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.
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
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
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
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.
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
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 = 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
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 = 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Shi Jin |
last post by:
Hi there,
While I was trying to disassemble a simple C program for my OS course, I
found that the call to function longjmp is actually called to siglongjmp
under linux.
And similarly, under...
|
by: lasek |
last post by:
Hi...in some posts i've read...something about using macro rather then
function...but difference ??.
Best regards....
|
by: baumann |
last post by:
hi all,
i defined a macro like the below
#define initSock() \
do{\
WSADATA ws_data;\
WSAStartup(0x0202,&ws_data);\
}while(0)
|
by: Simon Morgan |
last post by:
Hi,
Can somebody please help me grok the offsetof() macro?
I've found an explanation on
http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031
but I'm afraid it still...
|
by: Mockey Chen |
last post by:
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "
int main()
{
printf(LOG_PREFIX "some thing.\n");
return 0;
}
|
by: Umesh |
last post by:
how to convert a program to a function/macro and put it in a header
file? is there any shortcut method for this?
thanks.
|
by: jason |
last post by:
Hi,
I learned my lesson about passing pointers, but now I have a question
about macros.
Why does the function work and the MACRO which is doing the same thing
on the surface, does not work in...
|
by: Peng Yu |
last post by:
Hi,
It is benifitical to use macro in certain cases.
http://www.boost.org/doc/libs/1_35_0/libs/preprocessor/doc/index.html
However, I found that it is not easy to debug a macro. For example,...
|
by: mdh |
last post by:
I have asked a few questions about Macros...and think what I have been
missing ...and which all who have replied clearly understand...is that
this is a Pre-processor action.
Hopefully the above is...
|
by: sh.vipin |
last post by:
how to make large macro paste the code as it is
Problem Explanation '--
For example in the program below
/* a.c - starts here */
#define DECL_VARS() \
unsigned int a0;\
unsigned int a1;\...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |