Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 17-04-2008, 03:38 PM   #1 (permalink)
Linux Learner
 
PcEnthu's Avatar
 
Join Date: Mar 2008
Posts: 83
Wink 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
PcEnthu is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 17-04-2008, 03:51 PM   #2 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default 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!
mediator is offline  
Old 17-04-2008, 03:54 PM   #3 (permalink)
In The Zone
 
Kalyan's Avatar
 
Join Date: Nov 2005
Location: Earth
Posts: 241
Default 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
Kalyan is offline  
Old 17-04-2008, 03:55 PM   #4 (permalink)
Not fresh
 
VINSTAR's Avatar
 
Join Date: Aug 2007
Location: Adoor/Kerala/INDIA
Posts: 79
Default Re: Swap two numbers without using a temporary variable

x=x+y
y=x-y
x=x-y
__________________
gudz.org
VINSTAR is offline  
Old 17-04-2008, 03:58 PM   #5 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default 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
ray|raven is offline  
Old 17-04-2008, 03:58 PM   #6 (permalink)
Linux Learner
 
PcEnthu's Avatar
 
Join Date: Mar 2008
Posts: 83
Default 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
PcEnthu is offline  
Old 17-04-2008, 03:59 PM   #7 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 17-04-2008, 03:59 PM   #8 (permalink)
Wise Old Crow
 
blueshift's Avatar
 
Join Date: Apr 2005
Location: Inside the Pixel
Posts: 1,227
Default 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
blueshift is offline  
Old 17-04-2008, 04:03 PM   #9 (permalink)
Linux Learner
 
PcEnthu's Avatar
 
Join Date: Mar 2008
Posts: 83
Default Re: Swap two numbers without using a temporary variable

Quote:
Originally Posted by rayraven View Post
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
PcEnthu is offline  
Old 17-04-2008, 04:12 PM   #10 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default 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
ray|raven is offline  
Old 21-04-2008, 12:20 PM   #11 (permalink)
Right Off the Assembly Line
 
Join Date: Jan 2008
Posts: 7
Default 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
Jove is offline  
Old 22-04-2008, 10:27 AM   #12 (permalink)
In The Zone
 
Join Date: Oct 2006
Location: Mumbai
Posts: 365
Default 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.
Desi-Tek.com is offline  
Old 22-04-2008, 10:32 AM   #13 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default Re: Swap two numbers without using a temporary variable

Quote:
Originally Posted by Desi-Tek.com View Post
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
ray|raven is offline  
Old 22-04-2008, 10:46 AM   #14 (permalink)
Broken In
 
Join Date: Sep 2004
Location: Mysore
Posts: 137
Default 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..!!
hariharan is offline  
Old 22-04-2008, 10:54 AM   #15 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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 View Post
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
QwertyManiac is offline  
Old 22-04-2008, 10:59 AM   #16 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default 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
ray|raven is offline  
Old 22-04-2008, 02:43 PM   #17 (permalink)
In The Zone
 
Join Date: Oct 2006
Location: Mumbai
Posts: 365
Default 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.
Desi-Tek.com is offline  
Old 22-04-2008, 07:07 PM   #18 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default 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
aditya.shevade is offline  
Old 22-04-2008, 10:25 PM   #19 (permalink)
I have Yolks not Brains!
 
eggman's Avatar
 
Join Date: Aug 2006
Location: Inside the shell
Posts: 744
Default Re: Swap two numbers without using a temporary variable

Quote:
Originally Posted by Desi-Tek.com View Post
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?
eggman is offline  
Old 24-04-2008, 10:12 AM   #20 (permalink)
18 Till I Die............
 
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
Default Re: Swap two numbers without using a temporary variable

Quote:
Originally Posted by eggman View Post
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
mehulved is offline  
Old 28-04-2008, 09:42 PM   #21 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default 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
aditya.shevade is offline  
Old 29-04-2008, 01:22 PM   #22 (permalink)
C# Be Sharp !
 
Zeeshan Quireshi's Avatar
 
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
Default Re: Swap two numbers without using a temporary variable

In Ruby:
Code:
b,a = a,b
__________________
There are 10 types of people in the world: those who understand binary and those who do not.
Zeeshan Quireshi is offline  
Old 29-04-2008, 01:23 PM   #23 (permalink)
Think Zen.
 
ray|raven's Avatar
 
Join Date: Dec 2005
Posts: 1,498
Default Re: Swap two numbers without using a temporary variable

Quote:
Originally Posted by aditya.shevade View Post
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
ray|raven is offline  
Old 29-04-2008, 09:11 PM   #24 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: Swap two numbers without using a temporary variable

^^ yeah... I guess

Was it Mehul?
__________________
--- Console Junkie
aditya.shevade is offline  
Old 29-04-2008, 11:56 PM   #25 (permalink)
In The Zone
 
Vivek788's Avatar
 
Join Date: Jul 2006
Location: Cochin
Posts: 340
Default 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/
Vivek788 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
prob wid double variable prancks Programming 5 08-04-2008 04:11 PM
how add environment variable in ubuntu mak1012 Open Source 17 17-09-2007 05:48 PM
cant declare a string variable... geekgod Open Source 13 12-08-2006 06:09 AM
In Excel How to use cell ref as a variable abhijitroy Software Q&A 1 10-07-2006 02:32 PM
Temporary C++ amnesia 8| DKant Programming 7 07-02-2005 10:28 PM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 10:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2