The best way of swaping is
swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
}
________________________________
From: Prince Annan Koomson <[email protected]>
To: [email protected]
Sent: Tue, February 16, 2010 2:44:49 PM
Subject: Re: [c-prog] swap two numbers
The best way in swaping is using pointers that helps you more better than doing
the normal swaping.
Just try and pointers swaping is like you have three containers and there is
one container full of sand another container full of sugar and the third
container empty and you want to interchange the containers for the san
container to contain the sugar and the sugar container to contain the sand,
there you need a swaping to help you in this.
Just get it clearer by so doing you normally face problems when doing normal
swaping but it works better when using pointers.
I hope this helps.
Prince Annan Koomson
--- On Mon, 2/15/10, Thomas Hruska <thru...@cubiclesoft .com> wrote:
From: Thomas Hruska <thru...@cubiclesoft .com>
Subject: Re: [c-prog] swap two numbers
To: c-p...@yahoogroups. com
Cc: narayandabhadkar@ yahoo.com
Date: Monday, February 15, 2010, 6:40 AM
Dev Kashyap wrote:
> [mod-- Before replying to this post, please read the file
> swapnotemp.html in the html folder of the Files section
> of C-Prog... <http://tech. groups.yahoo. com/group/ c-prog/files>
> --mod PN]
>
> Hi friends!!
>
> i want to know what is the different methods to swap two numbers-
> one is using temp variable.
> second is by using xor
> third is by using + and -.
> plz tell me wheather there is another method to swaping.
>
> Thanks
> With Regards
> Dev kashyap
For those too lazy to log into Yahoo, here's a direct link:
http://f1.grp. yahoofs.com/ v1/4OJ4S_ cXRAttP226dTISG7 fYU4tgouBzcWcpJB
nnDGAOqYfy1Zfiph 9NmRQIEYtHP- fkgmFF5hreGXmCQ2 Yl/html/swapnote mp.html
To summarize: Don't do stupid cutesy tricks. Best case: You will
create broken code that appears to work most of the time. Worst case:
You will get fired. Or murdered - "Write code like the next person who
has to maintain it is a serial killer who knows where you live". There
is exactly ONE correct way to swap two numbers. Occam's razor.
You would be surprised how often this question comes up on this group
and how often we have to correct the many people who reply.
I'm CC'ing this to the teacher who was on this list recently wanting
problems for assignments. Teachers, this is the sort of question to NOT
ask students.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleS oft.com/MyTaskFo cus/
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]