In a C++ quiz, we were asked to write a program to swap two integer variables without using a third temporary variable.
Any suggestions??
9 7787
In a C++ quiz, we were asked to write a program to swap two integer variables without using a third temporary variable.
Any suggestions??
You can apply some logic like this:
b = a + b;
a = b - a;
b = b - a;
There may be other ways too.
Regards
Look up XOR swap.
I don't recommend using these swapping tricks though. The most optimal swapping algorithm is implicitly swapping the references to the variables, which a modern compiler will most likely do. You can't beat that with any gimmick you come up with.
Thank you very much but the one method which oler1s mentions, Does it use a temp var or not??
Well, what do you think, given that I was answering your question?
Plus, why didn't you actually look it up and see for yourself? You would know whether XOR swaps use temporary variables or not just by seeing the method online.
Please tone down. You have got me wrong. Thank you for the Swap Code . I got the code from the net. It works in the same way as the Add-swap does except it does the swap with bit operations.
Actually my query was about what you mentioned as ...
"The most optimal swapping algorithm is implicitly swapping the references to the variables, which a modern compiler will most likely do."
I would like you to focus on this point:there is not so much advantage in using XOR swap,consider this code written in MIPS/DLX assembly
[code]
lw $t1,BaseA(0) //we assume that our variables a,b are at postion
lw $t2,BaseB(0) // Mem[BaseA+0] and Mem[BaseB+0] respectively
so we could do something like this -
sw $t2,BaseC(0)
-
lw $t1,BaseC(0)
-
lw $t2,BaseA(0)
-
that is exactly the same number of istructions needed to do XOR Swap -
xor $t1,$t1,$t2
-
xor $t2,$t1,$t2
-
xor $t1,$t1,$t2
-
so I don't think XOR swap is so optimizing
Let's stick to C++ in this forum. MIPS/DLX is beyond me.
I'm sorry,you're right;let's stick to high level!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve |
last post by:
How do you rewrite the swap function without using a tmp variable in
the swap function????
int main()
{
int x = 3;
int y = 5;
// Passing by reference
|
by: JKop |
last post by:
union SignedChoice{
long with_sign;
unsigned long without_sign;
};
int main()
{
SignedChoice data;
|
by: OSHIMA |
last post by:
Hi,
I found the swap() function without a temporary variable.
void swap(int *x, int *y){
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
So, I wrote the next code that exchange the double.
|
by: Abhishek Jha |
last post by:
hello all,
this has been an interesting topic since long. you can swap two
numbers (say a and b) using temp(as third variable). you can also swap
without using third variable.likeThis was also...
|
by: pras.vaidya |
last post by:
hi, please help me with this problem : -
how to swap two addresses .For eg variable i is located at 2000 and j
at 3000 . Now i call swap function . Result should be that i should be
now having...
|
by: clidezzzZ |
last post by:
hi i want to know how to swap 2 variables in vb without using a temporary variable. thank you for the answer ^_^ god bless!!!!!
|
by: sachsase |
last post by:
//swap of 2 variables
void main()
{
int a=27,b=46;
a=1?a-(b=1?((a=1?a+b:0)-b):0):0;
printf("a=%db=%d",a,b);
}
I got output for this problem as a=46;b=27
Same but using scanf to read...
|
by: indrawati.yahya |
last post by:
OK, before you all stab me with your OT pitchfork, I just want to
mention that this question is indeed related to c.l.c++. My question
is, does the well-known method to swap two integers produce...
|
by: Chad |
last post by:
I hope this question doesn't appear twice. I tried posting it once,
however, I got a stupid 505 internal error on google. When this
happens, the post will either post 30 minutes later or won't post...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |