| View previous topic :: View next topic |
| Author |
Message |
smith2287 S.O.S. Newbie
Joined: 13 Oct 2009 Posts: 3
|
Posted: Tue, 13 Oct 2009 11:17:55 UTC Post subject: swap two variable? |
|
|
Hi all
If You are from side computer science then you are aware of swaping two variables
but i want t swap two variables without using temporary third variable. 
Last edited by smith2287 on Fri, 23 Oct 2009 07:31:53 UTC; edited 1 time in total |
|
| Back to top |
|
 |
Ilaggoodly Moderator

Joined: 15 Feb 2007 Posts: 668 Location: Bonn, Deutschland
|
Posted: Tue, 13 Oct 2009 12:19:21 UTC Post subject: |
|
|
first of all, a programming language would be a good idea. There is no general method for swapping variables without a temporary.
Actually I'm pretty sure its not possible, it doesn't really make sense how you could swap something without remembering it in between. Basically SOMETHING has to store the data somewhere else temporarily. _________________
 |
|
| Back to top |
|
 |
Shadow Moderator

Joined: 30 Mar 2005 Posts: 4528 Location: Urbana, IL
|
Posted: Tue, 13 Oct 2009 14:20:47 UTC Post subject: |
|
|
You want to use your logical operators am I right? This is actually a classic problem if so. _________________ (\ /)
(O.o)
(> <)
This is Bunny. Copy Bunny into your signature to help him on his way to world domination |
|
| Back to top |
|
 |
Denis Member of the 'S.O.S. Math' Hall of Fame
Joined: 24 Jul 2005 Posts: 2572 Location: Ottawa Ontario
|
Posted: Tue, 20 Oct 2009 14:47:30 UTC Post subject: |
|
|
Qbasic has command SWAP(a,b).
User does not need to use 3rd variable; however that doesn't eliminate the need! _________________ I upped my standards, so up yours ! |
|
| Back to top |
|
 |
aswoods S.O.S. Oldtimer

Joined: 23 Feb 2009 Posts: 289 Location: Adelaide, Australia
|
Posted: Tue, 20 Oct 2009 16:39:52 UTC Post subject: |
|
|
| Hint: no need for swap if uxorious |
|
| Back to top |
|
 |
Shadow Moderator

Joined: 30 Mar 2005 Posts: 4528 Location: Urbana, IL
|
Posted: Tue, 20 Oct 2009 20:58:30 UTC Post subject: |
|
|
| Ilaggoodly wrote: | first of all, a programming language would be a good idea. There is no general method for swapping variables without a temporary.
Actually I'm pretty sure its not possible, it doesn't really make sense how you could swap something without remembering it in between. Basically SOMETHING has to store the data somewhere else temporarily. |
Not true.
Let's say we have a and b. Let be x XOR y.
Then you just need to do:
 _________________ (\ /)
(O.o)
(> <)
This is Bunny. Copy Bunny into your signature to help him on his way to world domination |
|
| Back to top |
|
 |
nishaanth S.O.S. Newbie
Joined: 04 Nov 2009 Posts: 2 Location: student
|
Posted: Thu, 5 Nov 2009 13:45:42 UTC Post subject: |
|
|
x=x+y;
y=x-y;
x=x-y;
will do _________________ CODE UR LIFE |
|
| Back to top |
|
 |
|