• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

swaping of two numbers...help me

 
ramakrishna chowdary
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi this is ramakrishna chowdary . i am silent member of this javaranch . i want to know how to write swaping of two numbers with out using third variable. :roll:
 
Henry Wong
author
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you not want to use a temporary variable? Allocating a local int variable only takes 4 bytes on the stack.

Is this a puzzle question? If it is, then here is my answer...



BTW, please do *not* use this in your programs -- all that extra overhead to save 4 bytes is silly.

Henry


[HW: Deleted solution. Sorry, but could not tell if this is a homework problem or not. Please attempt to find solution first. See hints below]
[ September 16, 2006: Message edited by: Henry Wong ]
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

The possible reasons for this (in Java at least) would be a course assignment where the instructor posed the question for students to attempt to figure this out for themselves, including when one would and when one would not utilize this.

Or an interview question where the interviewer would want not only the answer but also if the interviewee knew the issues surrounding.
 
Henry Wong
author
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Fahlbusch:
The possible reasons for this (in Java at least) would be a course assignment where the instructor posed the question for students to attempt to figure this out for themselves, including when one would and when one would not utilize this.

Or an interview question where the interviewer would want not only the answer but also if the interviewee knew the issues surrounding.


Good point... I may have just done someone's homework problem. This is against the ranch's rules, so I'll go delete it.


Ramakrishna, here's a hint. It's a math problem... What operators have you learned in class? What have you tried to do?

Henry
[ September 16, 2006: Message edited by: Henry Wong ]
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[another homework solution deleted - Jim]
[ September 17, 2006: Message edited by: Jim Yingst ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a way to do it - using [ejfh: answer deleted, AGAIN]- it's in Ivor Horton's Beginning C++; but why would you want to do it without temporaries ?

Virtually every treatment of sorting will use temps, if and as needed.

This reminds me of my own self, re-inventing the wheel at three or four am.

Nowdays I just take sample code and slap it up there ... do you have a reason - other than inquiry and absorbing the problem and it's solutions. :roll:
[ September 17, 2006: Message edited by: Ernest Friedman-Hill ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently the previous discussion has not been sufficiently obvious to people. So, let's be clear:

DON'T POST COMPLETE SOLUTIONS TO THIS PROBLEM.

It is most likely a homework problem, and the original poster will learn much better if he figures at least part of it out for himself. Vague, partial solutions are OK. Complete solutions are not, and will be deleted. if you're not sure, then be more vague.
[ September 17, 2006: Message edited by: Jim Yingst ]
 
Sharath Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as for as i know it can done in two ways one an ordinary approach and other using xor operator

1> let the two variables be a and b

the two swap the values use the following

a=a+b;
b=a-b;
a=a-b;

example let a= 8 b=3

then
a=8+3;
b=11-3;//b=8
a=11-8;//a=3


the second approach i currently don't know


correct me if i am wrong
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic