• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

swaping of two numbers...help me

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[another homework solution deleted - Jim]
[ September 17, 2006: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic