 |
17-04-2008, 03:38 PM
|
#1 (permalink)
|
|
Linux Learner
Join Date: Mar 2008
Posts: 83
|
Swap two numbers without using a temporary variable
Ok guys. Here is the problem. There are two variables x and y which hold numbers. You have to swap the value of the two variables. U might say, just store the value of x in a temporary variable z, store value of y in x and finally store z value in y  Here is the tricky part, u have to do it without using the variable z
__________________
Its better ti keep your mouth shut and be thought as a fool, than to open and clear all doubts - [Source] Comments section of DistroWatch.Com
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
17-04-2008, 03:51 PM
|
#2 (permalink)
|
|
String Phreak
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
|
Re: Swap two numbers without using a temporary variable
x=a
y=b
x=x+y
y=x-y
x=x-y
show x,y
__________________
Bad Bad server.....No candy for u!
|
|
|
17-04-2008, 03:54 PM
|
#3 (permalink)
|
|
In The Zone
Join Date: Nov 2005
Location: Earth
Posts: 241
|
Re: Swap two numbers without using a temporary variable
x,y be the nos.
1) x=x+y
2) y=x-y
3) x=x-y
u got them swapped.
__________________
My config: AMD Athlon x2 5600+, Asus M2NPV-VM, Transcend 2x1GB 800MHz, XFX 8600GT 256MB, Seagate 250GB SATA2 + Seagate 500GB SATA2, LG 1752s LCD, Sony DRU v200A DVDRW, Logitech MX518, iBall Benz cab
|
|
|
17-04-2008, 03:55 PM
|
#4 (permalink)
|
|
Not fresh
Join Date: Aug 2007
Location: Adoor/Kerala/INDIA
Posts: 79
|
Re: Swap two numbers without using a temporary variable
x=x+y
y=x-y
x=x-y
__________________
gudz.org
|
|
|
17-04-2008, 03:58 PM
|
#5 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Swap two numbers without using a temporary variable
You can even use division and multiplication operators.
x=x*y
y=x/y
x=x/y
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
17-04-2008, 03:58 PM
|
#6 (permalink)
|
|
Linux Learner
Join Date: Mar 2008
Posts: 83
|
Re: Swap two numbers without using a temporary variable
@ all u guys, That was fast and u got that cracked that easily. Well done. I will try to come up with some level up questions later.
__________________
Its better ti keep your mouth shut and be thought as a fool, than to open and clear all doubts - [Source] Comments section of DistroWatch.Com
|
|
|
17-04-2008, 03:59 PM
|
#7 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Swap two numbers without using a temporary variable
Or if you wish to do it the l33t way, use XOR.
Starting with x=10, y=20
x^=y
y^=x
x^=y
Now x=20 and y=10.
Logic's the same as addition mostly, except its bitwise.
Edit: This a challenge or a homework question?  For challenges, there exists a thread, I hope you can revive it with better questions than the ones already present.
__________________
Harsh J
www.harshj.com
|
|
|
17-04-2008, 03:59 PM
|
#8 (permalink)
|
|
Wise Old Crow
Join Date: Apr 2005
Location: Inside the Pixel
Posts: 1,227
|
Re: Swap two numbers without using a temporary variable
// 'a' and 'b' are the two variables.
a=a^b
b=a^b
a=a^b
__________________
http://twitter.com/blueshift155
|
|
|
17-04-2008, 04:03 PM
|
#9 (permalink)
|
|
Linux Learner
Join Date: Mar 2008
Posts: 83
|
Re: Swap two numbers without using a temporary variable
Quote:
Originally Posted by rayraven
You can even use division and multiplication operators.
x=x*y
y=x/y
x=x/y
|
That would work for positive and negative numbers and not for zero
__________________
Its better ti keep your mouth shut and be thought as a fool, than to open and clear all doubts - [Source] Comments section of DistroWatch.Com
|
|
|
17-04-2008, 04:12 PM
|
#10 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Swap two numbers without using a temporary variable
^Yep. Thats one limitation, by using div.
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
21-04-2008, 12:20 PM
|
#11 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jan 2008
Posts: 7
|
Re: Swap two numbers without using a temporary variable
yeah...XORing is much better as compared to other suggested methods(here).
Other methods may not work if we are dealing with MAX possibles values of respective data types which results in overflow..so,
if(a!=b) ///if a==b, no need of swapping values
a^=b^=a^=b;
swaps the values in a and b
cheers
|
|
|
22-04-2008, 10:27 AM
|
#12 (permalink)
|
|
In The Zone
Join Date: Oct 2006
Location: Mumbai
Posts: 365
|
Re: Swap two numbers without using a temporary variable
x = 2
y = 4
y = y-x = 2
x = y+y = 4
__________________
Dhiraj Thakur
thakur.dheeraj(@)gmail.com
Last edited by Desi-Tek.com; 23-04-2008 at 03:37 PM.
|
|
|
22-04-2008, 10:32 AM
|
#13 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Swap two numbers without using a temporary variable
Quote:
Originally Posted by Desi-Tek.com
x = 2
y = 4
x = y-x = 2
y = x+x = 4
|
Lolz, that works only for those no's i guess.
Try these,
x=5
y=7
x = y-x = 7-5 = 2 ?
y = x+x = 2+2 = 4 ?
Crappy logic.
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
22-04-2008, 10:46 AM
|
#14 (permalink)
|
|
Broken In
Join Date: Sep 2004
Location: Mysore
Posts: 137
|
Re: Swap two numbers without using a temporary variable
i can do that in a single statement.. swap two number in w/o temp var
x ^= y ^= x ^= y;
This swaps the two varaibles... OF course in c and other languages which support bit wise operators..!!
|
|
|
22-04-2008, 10:54 AM
|
#15 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Swap two numbers without using a temporary variable
@rayraven - Lol!  By the way, D-Tek.com and Dheeraj are one and the same I think.
Quote:
Originally Posted by Desi-Tek.com
x = 2
y = 4
x = y-x = 2
y = x+x = 4
|
Hahah, moreover you started with x=2 and y=4 and ended with the same
__________________
Harsh J
www.harshj.com
Last edited by QwertyManiac; 22-04-2008 at 10:54 AM.
Reason: Automerged Doublepost
|
|
|
22-04-2008, 10:59 AM
|
#16 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Swap two numbers without using a temporary variable
^Lolz, i missed that part 
What was he thinking while writing it?
I've known guys who try to deduce logic from examples.
Crappy way, if you ask me. 99% of the time you end up with a logic that works only for the example.
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
22-04-2008, 02:43 PM
|
#17 (permalink)
|
|
In The Zone
Join Date: Oct 2006
Location: Mumbai
Posts: 365
|
Re: Swap two numbers without using a temporary variable
oh sorry i am awake from 24 hour
public
class Swap{
staticvoid swap(int x,int y){
//int temp=x;
x^=y; //2
y^=x; // 5
x^=y; //7
System.out.println("After swapping x = " + x + " y = " + y);
}
publicstaticvoid main(String[] args){
int x=5;
int y=7;
System.out.println("Before swapping x="+x+" y="+y);
swap(x,y);
}
}
//one line logic
public class Swap{
static void swap(int x,int y){
y=(x+y)-(x=y);
System.out.println("After swapping x= " + x + " y = " + y);
}
public static void main(String[] args){
int x=5;
int y=7;
System.out.println("Before swapping x="+x+" y="+y);
swap(x,y);
}
}
__________________
Dhiraj Thakur
thakur.dheeraj(@)gmail.com
Last edited by Desi-Tek.com; 22-04-2008 at 03:00 PM.
|
|
|
22-04-2008, 07:07 PM
|
#18 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
Re: Swap two numbers without using a temporary variable
LOL this has been discussed before....
anther way apart from the XOR way is,
b = a + b - (a = b); you are done.
__________________
--- Console Junkie
|
|
|
22-04-2008, 10:25 PM
|
#19 (permalink)
|
|
I have Yolks not Brains!
Join Date: Aug 2006
Location: Inside the shell
Posts: 744
|
Re: Swap two numbers without using a temporary variable
Quote:
Originally Posted by Desi-Tek.com
x = 2
y = 4
x = y-x = 2
y = x+x = 4
|
Ha ha ha ha ha ha ha ha ha ha ha ha ha ha
I've never seen a post funnier and noobier than this!!!!!
__________________
Y U NO ALLOW PICTURES IN SIGNATURES?
|
|
|
24-04-2008, 10:12 AM
|
#20 (permalink)
|
|
18 Till I Die............
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
|
Re: Swap two numbers without using a temporary variable
Quote:
Originally Posted by eggman
Ha ha ha ha ha ha ha ha ha ha ha ha ha ha
I've never seen a post funnier and noobier than this!!!!!
|
I have.
__________________
http://www.bash.org/?258908
|
|
|
28-04-2008, 09:42 PM
|
#21 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
Re: Swap two numbers without using a temporary variable
Oi mehul.... he said he has not... he did not say you have not....
__________________
--- Console Junkie
|
|
|
29-04-2008, 01:22 PM
|
#22 (permalink)
|
|
C# Be Sharp !
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
|
Re: Swap two numbers without using a temporary variable
__________________
There are 10 types of people in the world: those who understand binary and those who do not.
|
|
|
29-04-2008, 01:23 PM
|
#23 (permalink)
|
|
Think Zen.
Join Date: Dec 2005
Posts: 1,498
|
Re: Swap two numbers without using a temporary variable
Quote:
Originally Posted by aditya.shevade
Oi mehul.... he said he has not... he did not say you have not....
|
Sarcasm perhaps?
__________________
Do what you will; but not because you must. -- Zen Quote
|
|
|
29-04-2008, 09:11 PM
|
#24 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
Re: Swap two numbers without using a temporary variable
^^ yeah... I guess
Was it Mehul?
__________________
--- Console Junkie
|
|
|
29-04-2008, 11:56 PM
|
#25 (permalink)
|
|
In The Zone
Join Date: Jul 2006
Location: Cochin
Posts: 340
|
Re: Swap two numbers without using a temporary variable
well u can use these in a loop or condition statement like the if or for to avoid a semicolon.
__________________
Viva Green Computing!!
http://www.vivtech.blogspot.com/
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|