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 05-08-2011, 04:30 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Aug 2011
Posts: 3
Default Is it possible to swap two numbers without using a third temporary variable?


please answer if u can.
DIPANSHU_ASRI is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 05-08-2011, 06:07 PM   #2 (permalink)
Simply a DIGITian
 
krishnandu.sarkar's Avatar
 
Join Date: Nov 2007
Location: Kolkata
Posts: 2,942
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Yes

10chars
__________________
  • Read The Forum RULES First.
  • Before PM'ing Or Asking Any Questions To Any Mod Read The FAQ's
  • Before Starting A New Thread Read The STICKY THREADS First
  • Before Participating In Bazaar Section Read The BAZAAR RULES
krishnandu.sarkar is online now  
Old 05-08-2011, 08:09 PM   #3 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: Is it possible to swap two numbers without using a third temporary variable?

let x and y be two numbers that have some value.
x = x+y
y=x-y
x=x-y.
nims11 is online now  
Old 05-08-2011, 09:50 PM   #4 (permalink)
127.0.0.1
 
mitraark's Avatar
 
Join Date: Nov 2010
Location: Kolkata / Durgapur
Posts: 769
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Don;t you have a book or something ? You had to create a thread /?
__________________
Intel i5 760 || Gigabyte H55 || Corsair 4GB 1333MHz Value || XFX HD5670 1GB
WD 2TB Green + Seagate 5900RPM 2 TB + Seagate Freeagent 1 TB x2
Corsair CX400 || Zebronics Bijli || Samsung P2350 23.5" || Altec Lansing VS2621
My Speedtest :D
mitraark is online now  
Old 05-08-2011, 10:18 PM   #5 (permalink)
XLr8
 
arpanmukherjee1's Avatar
 
Join Date: Sep 2008
Posts: 637
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Code:
a= a^b;
b = b^a;
a = a^b;
__________________
Quote:
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.
arpanmukherjee1 is offline  
Old 06-08-2011, 10:29 AM   #6 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Is it possible to swap two numbers without using a third temporary variable?

First of all mention the programming language you are using.

If it's C or C++, above replies will do. Note don't try the one line XOR technique, that is undefined and not portable.

If it's Python
Code:
v1, v2 = v2, v1
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline  
Old 07-08-2011, 09:46 AM   #7 (permalink)
Omi
-=[-_-]=-
 
Join Date: Jul 2011
Posts: 148
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Use pointers
Omi is offline  
Old 07-08-2011, 10:52 AM   #8 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Quote:
Originally Posted by Omi View Post
Use pointers
i think it would still require another variable unless the address of two variables are consecutive.
nims11 is online now  
Old 07-08-2011, 11:58 AM   #9 (permalink)
Right Off the Assembly Line
 
ss max's Avatar
 
Join Date: Jul 2011
Location: Andromeda Galaxy
Posts: 41
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Quote:
Originally Posted by nims11 View Post
i think it would still require another variable unless the address of two variables are consecutive.
Yes it would require another variable because as soon as you will apply swap on pointer variable the previous value will be gone and you can not do both swaps in single operation.
For swapping follow the approach that nims11 provided.
ss max is offline  
Old 07-08-2011, 12:27 PM   #10 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: Is it possible to swap two numbers without using a third temporary variable?

Think I should assume the OP is using C/C++ and in that case the thread has run its course.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan 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


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:32 AM.


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

Search Engine Optimization by vBSEO 3.3.2