wood burning stoves*
The moose likes Java in General and the fly likes swaping of two numbers...help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "swaping of two numbers...help me " Watch "swaping of two numbers...help me " New topic
Author

swaping of two numbers...help me

ramakrishna chowdary
Greenhorn

Joined: Feb 11, 2006
Posts: 6
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 557
    
    7

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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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

Joined: Feb 26, 2006
Posts: 194
[another homework solution deleted - Jim]
[ September 17, 2006: Message edited by: Jim Yingst ]

-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Sharath Kumar
Greenhorn

Joined: Aug 15, 2006
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: swaping of two numbers...help me
 
Similar Threads
Swaping Primitive Data types in java
JTREE Data Swaping urgent help required
Directory swaping
swaping values from one list box to another
Swaping Numbers