Swapping Without Third Variable

Reply

Join Date: Sep 2007
Posts: 1
Reputation: argonaut is an unknown quantity at this point 
Solved Threads: 0
argonaut's Avatar
argonaut argonaut is offline Offline
Newbie Poster

Swapping Without Third Variable

 
0
  #1
Sep 11th, 2007

hi all!!
i am a newbie in this forum.
can u solve me the problem of interchanging/swapping the value of two varibles without using the third variable...
thanks..
Last edited by WolfPack; Sep 11th, 2007 at 4:52 am. Reason: CHANGED ALL UPPERCASE TO LOWERCASE.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Swapping Without Third Variable

 
0
  #2
Sep 11th, 2007
http://www.catb.org/~esr/faqs/smart-...html#writewell

Yes you can http://c-faq.com/expr/xorswapexpr.html
But that is a dumb trick which ceased to be of any importance when we came down from the trees and started using high level programming languages.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Swapping Without Third Variable

 
0
  #3
Sep 11th, 2007
Here's anothe way
  1. x -= y;
  2. y += x; // y becomes x
  3. x = (y - x); // x becomes y
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Swapping Without Third Variable

 
0
  #4
Sep 11th, 2007
Ever hear of arithmetic overflow ?

- it would be OK on unsigned types.
- it would be liable to arithmetic overflow on signed types.
- for floating point types, expect both to become corrupted, especially if values differ greatly in magnitude
- utterly hopeless as an approach for any other type.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Swapping Without Third Variable

 
0
  #5
Sep 11th, 2007
Is it that time of year again?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Swapping Without Third Variable

 
0
  #6
Sep 14th, 2007
I would really suggest to use the temp variable to swap two values. Cos the swapping variables without third variable such as using the arithmetic operators. This would really very help only when you wanted to swap unsigned integers. Apart from that it is not a recommended at all. If anything other unsigned integers you need to use the temp variable to swap. But in over all the latter would be efficient way of doing it.

ssharish
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 63
Reputation: alsoumhi is an unknown quantity at this point 
Solved Threads: 0
alsoumhi alsoumhi is offline Offline
Junior Poster in Training

Re: Swapping Without Third Variable

 
0
  #7
Sep 17th, 2007
I have another way to solve your problem

1. x=x+y;
2. y=x-y;
3. x=x-y;
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Swapping Without Third Variable

 
0
  #8
Sep 17th, 2007
> I have another way to solve your problem
Apparently, you didn't read reply #4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC