freelanceprogrammers.org Forum Index » C
Help about swapping
Joined: 23 Dec 2004
Posts: 2
Help about swapping
Dear Vijay,
I had accedently delete your mail,sorry for that.But I am posting it
again.
Rgds
$hyam
From: vijay kalkundri <vvkalkundri@...>
Date: Tue Dec 21, 2004 6:55pm
Subject: Help about swapping
hi everybody
can u suggest me a way to swap two variables without using third vaiable and it
should even work when the integers are 32767 and 32768 (keep it in mind) before
answering
thanking u
urs truly
vijay.v.k
Joined: 23 Dec 2004
Posts: 2
Help about swapping
Hi
I will go for using ^ operator rather than + and - becoz + could cause
an overflow of the sum. I would rather do it this way :
a = a^b;
b=a^b;
a=a^b;
Atleast this way there is no possiblity of causing an overflow and
losing the value..
Thanks!! and have a nice day!!
Jaspreet
P. S.: Btw what do u mean by bottom posting.. Shd I have my reply after
the message. I thought u shd always have ur replies either embedded in
the message (better approach) or at the top...
-----Original Message-----
From: Shyam [mailto:shyammec@...]
Sent: Thursday, December 23, 2004 5:15 PM
To: Programmers-Town@yahoogroups.com
Subject: (PT) Help about swapping
Dear Vijay,
I had accedently delete your mail,sorry for that.But I am
posting it again.
Rgds
$hyam
From: vijay kalkundri <vvkalkundri@...>
Date: Tue Dec 21, 2004 6:55pm
Subject: Help about swapping
hi everybody
can u suggest me a way to swap two variables without using third vaiable
and it
should even work when the integers are 32767 and 32768 (keep it in mind)
before answering
thanking u
urs truly
vijay.v.k
To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Yahoo! Groups Links
################################################################################
#####
Note:
This message is for the named person`s use only. It may contain confidential,
proprietary or legally privileged information. No confidentiality or privilege
is waived or lost by any mistransmission. If you receive this message in error,
please immediately delete it and all copies of it from your system, destroy any
hard copies of it and notify the sender. You must not, directly or indirectly,
use, disclose, distribute, print, or copy any part of this message if you are
not
the intended recipient. MINDTECK (INDIA) LTD and any of its subsidiaries each
reserve
the right to monitor all e-mail communications through its networks.
Any views expressed in this message are those of the individual sender, except
where
the message states otherwise and the sender is authorized to state them to be
the
views of any such entity.
Thank You.
################################################################################
#####
Joined: 23 Dec 2004
Posts: 1
Help about swapping
HI
if sum of both the integers is lessthan the max limit the method is
Let we have two variables a,b the steps are:-
a=a+b;
b=a-b;
a=a-b;
but if we have a+b greater than the max limit than integers can be swaped by analysing the two variables bit-by-bit . i.e if you find difference in the bits change fron 0 to 1 and vice-versa.
I think this should work comfortable.
Regards
Gaurav Wadhwa
Shyam <shyammec@...> wrote:
Dear Vijay, I had accedently delete your mail,sorry for that.But I am posting it again.Rgds$hyamFrom: vijay kalkundri Date: Tue Dec 21, 2004 6:55pmSubject: Help about swappinghi everybodycan u suggest me a way to swap two variables without using third vaiable and itshould even work when the integers are 32767 and 32768 (keep it in mind) before answeringthanking uurs trulyvijay.v.k------------------------ Yahoo! Groups Sponsor --------------------~--> $4.98 domain names from Yahoo!. Register anything.http://us.click.yahoo.com/Q7_YsB/neXJAA/yQLSAA/EbFolB/TM--------------------------------------------------------------------~-> To unsubscribe : programmers-town-unsubscribe@yahoogroups.comYahoo! Groups
Links<*> To visit your group on the web, go to:http://groups.yahoo.com/group/Programmers-Town/<*> To unsubscribe from this group, send an email to:Programmers-Town-unsubscribe@yahoogroups.com<*> Your use of Yahoo! Groups is subject to:http://docs.yahoo.com/info/terms/
Yahoo! India Matrimony: Find your life partner
online.
Joined: 24 Dec 2004
Posts: 1
Help about swapping
From: vijay kalkundri <vvkalkundri@...>
Date: Tue Dec 21, 2004 6:55pm
Subject: Help about swapping
hi everybody
can u suggest me a way to swap two variables without
using third vaiable and it
should even work when the integers are 32767 and 32768
(keep it in mind) before answering
thanking u
urs truly
vijay.v.k
HAI TO EVERYBODY.
I`M, SIVA KRISHNA CHAITANYA.Y, A SILENT MEMBRE OF THIS
WONDERFULL GROUP.
DEAR VIJAY,
THE ABOVE QUESTION CAN BE ANSWEREDIN TWO WAYS WITHOUT
USING A THIRD VARIABLE .
THEY ARE AS FOLLOWS.
1. BY USING ARTHEMATIC OPERATOERS
-------------------------------
CONSIDER TWO VARIABLES a AND b.
THEIR VALUES CAN BE INTERCHANGED AS FOLLOWS.
a=a+b;
b=a-b;
a=a-b;
BUT HERE, AS YOU SAID, THE CONDITIONS MUST BE
TAKEN CARE.
BUT IN THE SECOND CASE NO NEED OF THAT.
2. BY USING LOGICAL OPERATORS
---------------------------
a=a^b;
b=a^b;
a=a^b;
NOW CHECK THIS.
IT WORKS FOR ALMOSTAL EVERY CASE.
I ALWAYS PREFER THE SECOND ONE.
HAVE A NICE TIME.
BYE
SIVA
KRISHNA CHAITANYA YADLAPALLI
__________________________________
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.
http://celebrity.mail.yahoo.com
Joined: 24 Dec 2004
Posts: 3
Help about swapping
hi vijay,
This is the code for swapping:-
void swap(int &a, int &b)
{
a=a+b;
a=a-b;
b=a-b;
cout<<"
a is"<<a<<"
b is"<<b;
}
--- Shyam <shyammec@...> wrote:
>
>
>
> Dear Vijay,
> I had accedently delete your mail,sorry
> for that.But I am posting it again.
>
> Rgds
> $hyam
>
>
> From: vijay kalkundri <vvkalkundri@...>
> Date: Tue Dec 21, 2004 6:55pm
> Subject: Help about swapping
>
> hi everybody
>
> can u suggest me a way to swap two variables without
> using third vaiable and it
> should even work when the integers are 32767 and
> 32768 (keep it in mind) before answering
>
>
> thanking u
> urs truly
> vijay.v.k
>
>
>
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Joined: 03 Jan 2005
Posts: 10
Help about swapping
Jaspreet Oberoi wrote:
> P. S.: Btw what do u mean by bottom posting.. Shd I have my reply after
> the message. I thought u shd always have ur replies either embedded in
> the message (better approach) or at the top...
>
See Point 2 below for your answer.
Embedded replies are acceptable wherever required, but top postings are
hated.
Kapil
--
PS:
1. Google: Feel free to Google before posting. http://www.google.com
2. Posting: I suggest the group members to avoid top posting, as this
pushes the original question to the bottom. While any body would prefer
to have the main question at the top, and subsequent replies added to
the bottom, so that it is easy to comprehend the flow of information.
3. Do not Hijack threads. If you need to ask a fresh question, do a
little effort of moving mouse to the New or Compose button instead of
Reply. Do not start a New message or thread by hitting "Reply" in your
mail client and changing the subject. When you do this, you mess up
other participant`s ability to read mail in a threaded fashion.
4. HTML Mail: HTML is not email, and email doesn`t contain HTML, so
please turn HTML formatting OFF in your email client. Please Do not use
italics, colors, bold, fonts, pictures, sounds, or other HTML elements.
Please use only 7-bit text when sending email to the lists.
Thanks
Joined: 29 Dec 2004
Posts: 31
Help about swapping
From: harry potter [mailto:mummy_returns_arnold@...]
>hi vijay,
>
>This is the code for swapping:-
>
>void swap(int &a, int &b)
>{
>a=a+b;
>a=a-b;
>b=a-b;
>cout<<"
a is"<<a<<"
b is"<<b;
>}
This breaks in two instances:
#include <limits.h>
int x=y=INT_MAX, z=10;
//(1)
swap(x, y); // undefined behaviour
//(2)
swap(z, z); // z==0 after this call.
--
PJH
"...one of the main causes of the fall of the Roman Empire was that,
lacking zero, they had no way to indicate successful termination of
their C programs." - Robert Firth
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : sales@... Service :
service@...
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Joined: 29 Dec 2004
Posts: 31
Help about swapping
From: Jaspreet Oberoi [mailto:jaspreet.oberoi@...]
>Hi
>I will go for using ^ operator rather than + and - becoz + could cause
>an overflow of the sum. I would rather do it this way :
>
>a = a^b;
>b=a^b;
>a=a^b;
>
>Atleast this way there is no possiblity of causing an overflow and
>losing the value..
.. however the result of doing this on anything except integers is
undefined in C and C++.
>Thanks!! and have a nice day!!
>Jaspreet
>
>P. S.: Btw what do u mean by bottom posting.. Shd I have my reply after
>the message. I thought u shd always have ur replies either embedded in
>the message (better approach) or at the top...
It`s called `top posting` (or bottom quoting) actually. See my sig for
an explanation...
--
PJH
It makes it difficult to follow the thread of conversation.
What`s wrong with top posting?
Top posting.
What`s the most annoying thing on email lists?
Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272
Website : www.alderley.com Sales : sales@... Service :
service@...
This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...
This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Joined: 31 Dec 2004
Posts: 5
Help about swapping
the answer is quite on expected lines:
when you add maximum value of an integer with itself again then you are going to get a number which is greater than an integer data type.hence you get an unexpected result.
coming to saecond swap stmt:
yor are accepting z as refernec so whatever value is there will be reflected back in the main pgm:
a=10;
b=10;
a=a+b=10+10=20;
a=a-b=20-10=10;b=a-b=10-10=0;
henmce we are getting z value as 0Paul Herring <pherring@...> wrote:
From: harry potter [mailto:mummy_returns_arnold@...] >hi vijay,>>This is the code for swapping:->>void swap(int &a, int &b)>{>a=a+b;>a=a-b;>b=a-b;>cout<<"
a is"<<a<<"
b is"<<b;>}This breaks in two instances:#include <limits.h>int x=y=INT_MAX, z=10;//(1)swap(x, y); // undefined behaviour//(2)swap(z, z); // z==0 after this call.--PJH"...one of the main causes of the fall of the Roman Empire was that,lacking zero, they had no way to indicate successful termination oftheir C programs." - Robert Firth Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 8JD, UKTel: +44(0)1454 294556 Fax: +44 (0)1454 299272Website :
www.alderley.com Sales : sales@... Service : service@...This email and its contents are confidential and are solely for the use of the intended recipient. If you are not the original recipient you have received it in error and any use, dissemination, forwarding, printing or copying of this email is strictly prohibited. Should you receive this email in error please immediately notify it@...This email has been scanned for viruses, however you should always scan emails with your own systems prior to opening.To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more.
Joined: 25 Jul 2003
Posts: 48
Help about swapping
Reply embedded...
> -----Original Message-----
> From: Jaspreet Oberoi [mailto:jaspreet.oberoi@...]
> Sent: Thursday, December 23, 2004 6:14 AM
> To: Programmers-Town@yahoogroups.com
> Subject: RE: (PT) Help about swapping
>
>
>
> Hi
> I will go for using ^ operator rather than + and - becoz + could cause
> an overflow of the sum. I would rather do it this way :
>
> a = a^b;
> b=a^b;
> a=a^b;
>
> Atleast this way there is no possiblity of causing an overflow and
> losing the value..
This only works for integral value and only if `a` and `b` are different.
Shyan
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







