944,066 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5173
  • C RSS
Sep 11th, 2007
0

Swapping Without Third Variable

Expand Post »

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
argonaut is offline Offline
1 posts
since Sep 2007
Sep 11th, 2007
0

Re: Swapping Without Third Variable

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 11th, 2007
0

Re: Swapping Without Third Variable

Here's anothe way
  1. x -= y;
  2. y += x; // y becomes x
  3. x = (y - x); // x becomes y
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 11th, 2007
0

Re: Swapping Without Third Variable

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 11th, 2007
0

Re: Swapping Without Third Variable

Is it that time of year again?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 14th, 2007
0

Re: Swapping Without Third Variable

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
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Sep 17th, 2007
0

Re: Swapping Without Third Variable

I have another way to solve your problem

1. x=x+y;
2. y=x-y;
3. x=x-y;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
alsoumhi is offline Offline
66 posts
since Aug 2007
Sep 17th, 2007
0

Re: Swapping Without Third Variable

> I have another way to solve your problem
Apparently, you didn't read reply #4
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: New Experiment Messin With
Next Thread in C Forum Timeline: Huffman Coding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC