S.O.S. Mathematics CyberBoard Forum Index S.O.S. Mathematics CyberBoard
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
swap two variable?

 
Post new topic   Reply to topic    S.O.S. Mathematics CyberBoard Forum Index -> Computer Science
View previous topic :: View next topic  
Author Message
smith2287
S.O.S. Newbie


Joined: 13 Oct 2009
Posts: 3

PostPosted: Tue, 13 Oct 2009 11:17:55 UTC    Post subject: swap two variable? Reply with quote

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. Question


Last edited by smith2287 on Fri, 23 Oct 2009 07:31:53 UTC; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
Ilaggoodly
Moderator


Joined: 15 Feb 2007
Posts: 668
Location: Bonn, Deutschland

PostPosted: Tue, 13 Oct 2009 12:19:21 UTC    Post subject: Reply with quote

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
View user's profile Send private message
Shadow
Moderator


Joined: 30 Mar 2005
Posts: 4528
Location: Urbana, IL

PostPosted: Tue, 13 Oct 2009 14:20:47 UTC    Post subject: Reply with quote

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
View user's profile Send private message
Denis
Member of the 'S.O.S. Math' Hall of Fame


Joined: 24 Jul 2005
Posts: 2572
Location: Ottawa Ontario

PostPosted: Tue, 20 Oct 2009 14:47:30 UTC    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
aswoods
S.O.S. Oldtimer


Joined: 23 Feb 2009
Posts: 289
Location: Adelaide, Australia

PostPosted: Tue, 20 Oct 2009 16:39:52 UTC    Post subject: Reply with quote

Hint: no need for swap if uxorious
Back to top
View user's profile Send private message
Shadow
Moderator


Joined: 30 Mar 2005
Posts: 4528
Location: Urbana, IL

PostPosted: Tue, 20 Oct 2009 20:58:30 UTC    Post subject: Reply with quote

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
View user's profile Send private message
nishaanth
S.O.S. Newbie


Joined: 04 Nov 2009
Posts: 2
Location: student

PostPosted: Thu, 5 Nov 2009 13:45:42 UTC    Post subject: Reply with quote

x=x+y;
y=x-y;
x=x-y;
will do
_________________
CODE UR LIFE
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    S.O.S. Mathematics CyberBoard Forum Index -> Computer Science All times are UTC
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Contact Us | S.O.S. Mathematics Homepage
Privacy Statement | Search the "old" CyberBoard

users online during the last hour
Powered by phpBB © 2001, 2005-2009 phpBB Group.
Installation and all modifications: H. Knaust
Copyright © 1999-2009 MathMedics, LLC. All rights reserved.
Math Medics, LLC. - P.O. Box 12395 - El Paso TX 79913 - USA