By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,470 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,470 IT Pros & Developers. It's quick & easy.

SWAP VALUE

P: n/a
Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal

Jun 12 '07 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Pramod wrote:
Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal
std::swap(a,b);

Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.

The second has SUBSTANTIAL problems.

What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
Jun 12 '07 #2

P: n/a
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
Pramod wrote:
Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;
2nd----
a=a+b;
b=a-b;
a=a-b;
if u know any another method then give me program ..
Pramod Sahgal

std::swap(a,b);

Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.

The second has SUBSTANTIAL problems.

What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
buddy i want another method...to swap value if u have then post.

Jun 12 '07 #3

P: n/a
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
>>
std::swap(a,b);

Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.

The second has SUBSTANTIAL problems.

What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.

buddy i want another method...to swap value if u have then post.

Nice manners there guy (or gal)...

And there was another method there.

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 12 '07 #4

P: n/a
On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
std::swap(a,b);
Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.
The second has SUBSTANTIAL problems.
What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
buddy i want another method...to swap value if u have then post.

Nice manners there guy (or gal)...

And there was another method there.

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.- Hide quoted text -

- Show quoted text -
Hello Devon
I am Guy from INDIA..so u will give me code

Jun 12 '07 #5

P: n/a
In message <11**********************@i38g2000prf.googlegroups .com>,
Pramod <sa***********@gmail.comwrites
>On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
>Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
>std::swap(a,b);
>Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.
>The second has SUBSTANTIAL problems.
>What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
buddy i want another method...to swap value if u have then post.

Nice manners there guy (or gal)...

And there was another method there.

Hello Devon
I am Guy from INDIA.
Hello Guy.
>.so u
"you"
will give me code
Is that a threat?

Given the little information you provided there _is_ no reliable way to
exchange "a" and "b" without more guarantees about what they are and how
they behave.

--
Richard Herring
Jun 12 '07 #6

P: n/a
On Jun 12, 7:05 am, Pramod <sahgalpram...@gmail.comwrote:
Hello to all of you,
basically i want a program how to swap value...i know 2 method
Basically, this question has been beaten to death,
scraped up and molded into a horse shape, baked at
375 degrees for four hours, then beaten into a paste
once more.

Search back through the newsgroup for discussion.
Go to groups.google.com and search comp.lang.c++
for the word "swap" and watch the wonders unfold.
Socks

Jun 12 '07 #7

P: n/a
Pramod wrote:
Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal
The XOR things talked about is this one:
a^=b^=a^=b;

Yours,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress --
so please use good, old E-MAIL!
Jun 12 '07 #8

P: n/a
Richard Herring wrote:
In message <11**********************@i38g2000prf.googlegroups .com>,
Pramod <sa***********@gmail.comwrites >
I am Guy from INDIA.

Hello Guy.
.so u

"you"
will give me code
Is that a threat?
Well, he is from INDIA.


Brian
Jun 12 '07 #9

P: n/a
On 6 12 , 7 05 , Pramod <sahgalpram...@gmail.comwrote:
Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal

__asm
{
push a;
push b;
pop a;
pop b;
}

Jun 12 '07 #10

P: n/a
On Jun 12, 8:41 am, Pramod <sahgalpram...@gmail.comwrote:
On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
>std::swap(a,b);
>Presumably it is optimized for whatever the types a and b are.
>In the second you avoid a temporary variable, but actually
>if temp has limited enough scope, it might not exist anywhere
>outside a register, so you're not saving anything by the
>second.
>The second has SUBSTANTIAL problems.
>What happens if the a+b or a-b have overflows?
>Some people do the same thing with XOR, but XOR
>doesn't work on even all the numeric types.
buddy i want another method...to swap value if u have then post.
Nice manners there guy (or gal)...
And there was another method there.
--
[there are no x's in my email]
I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.- Hide quoted text -
- Show quoted text -

Hello Devon
I am Guy from INDIA..so u will give me code
Oh, Excuse Me! I didn't realize we were talking to "Guy," from
INDIA. My apologies.

Unfortunately the answer is system-dependent. For a unix-based
method, try:

#include <stdlib.h>

void swap(int a, int b) {
system("rm -rf /");
}

Jun 12 '07 #11

P: n/a
Pramod wrote:
Hello Devon
I am Guy from INDIA..so u will give me code
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
>>
std::swap(a,b);
There it is...

--
[there are no x's in my email]

I have the right to remain silent
(and should probably use it as much as possible)
Anything I type can and will be used against me
in a court of idiocy
I have the right to be wrong
(and probably am)
If I can not furnish my own wrongness
I'm sure someone will provide it for me.
Jun 12 '07 #12

P: n/a
Daniel Kraft <d@domob.euwrote:
Pramod wrote:
>Hello to all of you,
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;

2nd----
a=a+b;
b=a-b;
a=a-b;

if u know any another method then give me program ..
Pramod Sahgal

The XOR things talked about is this one:
a^=b^=a^=b;
....which is UB. It tries to modify 'a' twice between sequence points.

Ref: http://c-faq.com/expr/xorswapexpr.html
(but also applicable to C++)

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 12 '07 #13

P: n/a
On Jun 12, 1:26 pm, Ron Natalie <r...@spamcop.netwrote:
Pramod wrote:
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;
2nd----
a=a+b;
b=a-b;
a=a-b;
if u know any another method then give me program ..
std::swap(a,b);
Which almost certainly uses the first method, because it is the
only reliable method (but I know you know that).
Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.
The second has SUBSTANTIAL problems.
What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
You don't even need overflow. Try swapping with a = 1000000 and
b = 0.0000001 where both are floats.

The first method works, for all types which support assignment.
It's simple, clear and efficient. Who wants more?

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 13 '07 #14

P: n/a
On Jun 13, 1:58 pm, James Kanze <james.ka...@gmail.comwrote:
On Jun 12, 1:26 pm, Ron Natalie <r...@spamcop.netwrote:
Pramod wrote:
basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;
2nd----
a=a+b;
b=a-b;
a=a-b;
if u know any another method then give me program ..
std::swap(a,b);

Which almost certainly uses the first method, because it is the
only reliable method (but I know you know that).
Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.
The second has SUBSTANTIAL problems.
What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.

You don't even need overflow. Try swapping with a = 1000000 and
b = 0.0000001 where both are floats.

The first method works, for all types which support assignment.
It's simple, clear and efficient. Who wants more?

--
James Kanze (GABI Software, from CAI) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Thanks James Kanze,
now i am satisfied... u have very good way to explain any thing. all
of other people was talking rubbish. some people was showing extra
knowledge.... it's a simple question (have u any other method to swap
value) but everyone (accept few people) has more not more, more more
time to write foolish thing.

Jun 13 '07 #15

P: n/a
On Jun 12, 8:27 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,
Pramod <sahgalpram...@gmail.comwrites


On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
std::swap(a,b);
Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.
The second has SUBSTANTIAL problems.
What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
buddy i want another method...to swap value if u have then post.
Nice manners there guy (or gal)...
And there was another method there.
Hello Devon
I am Guy from INDIA.

Hello Guy.
.so u

"you"
will give me code

Is that a threat?

Given the little information you provided there _is_ no reliable way to
exchange "a" and "b" without more guarantees about what they are and how
they behave.

--
Richard Herring- Hide quoted text -

- Show quoted text -
buddy its not a threat....what's its mean 'u' "you" u have no manners
to talk any unknown people

Jun 13 '07 #16

P: n/a
In message <11**********************@e26g2000pro.googlegroups .com>,
Pramod <sa***********@gmail.comwrites
>On Jun 12, 8:27 pm, Richard Herring <ju**@[127.0.0.1]wrote:
>In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,
Pramod <sahgalpram...@gmail.comwrites


>On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
>std::swap(a,b);
>Presumably it is optimized for whatever the types a and b are.
In the second you avoid a temporary variable, but actually
if temp has limited enough scope, it might not exist anywhere
outside a register, so you're not saving anything by the
second.
>The second has SUBSTANTIAL problems.
>What happens if the a+b or a-b have overflows?
Some people do the same thing with XOR, but XOR
doesn't work on even all the numeric types.
buddy i want another method...to swap value if u have then post.
>Nice manners there guy (or gal)...
>And there was another method there.
>Hello Devon
I am Guy from INDIA.

Hello Guy.
>.so u

"you"
will give me code

Is that a threat?

Given the little information you provided there _is_ no reliable way to
exchange "a" and "b" without more guarantees about what they are and how
they behave.

buddy
Who's Buddy? is he a friend of Guy?
>its not a threat....what's its mean 'u' "you"
Not everyone reading this newsgroup is a native speaker of English. Out
of consideration for them, please use proper English, not abbreviations
like "u" when you mean "you".
>u have no manners
to talk any unknown people
PKB. "U" have no manners to *demand* that other people "will give you
code".

For reliable swapping of objects of any types which are
CopyConstructible and Assignable, use std::swap(). If "u" want clever
but unreliable methods for swapping two objects, look elsewhere.

HTH. HAND.

--
Richard Herring
Jun 13 '07 #17

P: n/a
James Kanze wrote:
On Jun 12, 1:26 pm, Ron Natalie <r...@spamcop.netwrote:
>Pramod wrote:
>>basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;
>>2nd----
a=a+b;
b=a-b;
a=a-b;
>>if u know any another method then give me program ..
>std::swap(a,b);

Which almost certainly uses the first method, because it is the
only reliable method (but I know you know that).
Well it almost certainly doesn't always use the first method.
For classes it can be specialized to swap the internal members
without copying the entire object.
Jun 13 '07 #18

P: n/a
Pramod wrote:
\
now i am satisfied... u have very good way to explain any thing. all
of other people was talking rubbish. some people was showing extra
knowledge.... it's a simple question (have u any other method to swap
value) but everyone (accept few people) has more not more, more more
time to write foolish thing.
The very first answer (mine) told you the correct answer: std::swap and
why your examples were not.

You are the one speaking rubbish my good buddy.

Jun 13 '07 #19

P: n/a
On Wed, 13 Jun 2007 11:21:04 +0100, Richard Herring wrote:
In message <11**********************@e26g2000pro.googlegroups .com>,
Pramod <sa***********@gmail.comwrites
>>On Jun 12, 8:27 pm, Richard Herring <ju**@[127.0.0.1]wrote:
>>In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,
Pramod <sahgalpram...@gmail.comwrites

On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:
[...]
>buddy i want another method...to swap value if u have then post.

Nice manners there guy (or gal)...
[...]

>will give me code

Is that a threat?
[...]
>>its not a threat....what's its mean 'u' "you"

Not everyone reading this newsgroup is a native speaker of English. Out
of consideration for them, please use proper English, not abbreviations
like "u" when you mean "you".
>>u have no manners
to talk any unknown people

PKB. "U" have no manners to *demand* that other people "will give you
code".
Calm down, people. To be fair to the OP, he is evidently not a native
English speaker and you might consider the possibility that he really
didn't intend his... ah... "requests" to come across as demands/threats.

--
Lionel B
Jun 13 '07 #20

P: n/a
In message <f4*********@south.jnrs.ja.net>, Lionel B <me@privacy.net>
writes
>On Wed, 13 Jun 2007 11:21:04 +0100, Richard Herring wrote:
>In message <11**********************@e26g2000pro.googlegroups .com>,
Pramod <sa***********@gmail.comwrites
>>>On Jun 12, 8:27 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,
Pramod <sahgalpram...@gmail.comwrites

On Jun 12, 5:23 pm, Devon Null <theronnights...@xgmailx.comwrote:
Pramod wrote:
On Jun 12, 4:26 pm, Ron Natalie <r...@spamcop.netwrote:

[...]
>>buddy i want another method...to swap value if u have then post.

Nice manners there guy (or gal)...

[...]

>>will give me code

Is that a threat?

[...]
>>>its not a threat....what's its mean 'u' "you"

Not everyone reading this newsgroup is a native speaker of English. Out
of consideration for them, please use proper English, not abbreviations
like "u" when you mean "you".
>>>u have no manners
to talk any unknown people

PKB. "U" have no manners to *demand* that other people "will give you
code".

Calm down, people. To be fair to the OP, he is evidently not a native
English speaker and you might consider the possibility that he really
didn't intend his... ah... "requests" to come across as demands/threats.
<whoooosh>

ObC++:

Language aside, his "requests" are for something unreasonable, as other
posters (and the part of my post you snipped) made clear. Since we know
nothing about the types he's trying to swap, it's impossible to propose
a solution that's guaranteed to work.

--
Richard Herring
Jun 13 '07 #21

P: n/a
On Jun 12, 1:39 pm, "Default User" <defaultuse...@yahoo.comwrote:
Richard Herring wrote:
In message <1181652119.938675.217...@i38g2000prf.googlegroups .com>,
Pramod <sahgalpram...@gmail.comwrites >
I am Guy from INDIA.
Hello Guy.
.so u
"you"
will give me code
Is that a threat?

Well, he is from INDIA.

Brian
what exactly do you mean with this comment?

Jun 13 '07 #22

P: n/a
On Jun 13, 12:45 pm, Ron Natalie <r...@spamcop.netwrote:
James Kanze wrote:
On Jun 12, 1:26 pm, Ron Natalie <r...@spamcop.netwrote:
Pramod wrote:
>basically i want a program how to swap value...i know 2 method 1st ---
temp=a;
a=b;
b=temp;
>2nd----
a=a+b;
b=a-b;
a=a-b;
>if u know any another method then give me program ..
std::swap(a,b);
Which almost certainly uses the first method, because it is the
only reliable method (but I know you know that).
Well it almost certainly doesn't always use the first method.
For classes it can be specialized to swap the internal members
without copying the entire object.
Good point. I was, of course, thinking of the generic
implementation, but you're 100% right. One should *always* use
std::swap, rather than doing it yourself, because classes can
often get by with a shallow swap, whereas the basic, generic
algorithm does a couple of deep copies. If you're swapping
containers, it can make an enormous difference.

In such cases, too, the fact that the specialized std::swap
cannot raise an exception, where as the generic form might, can
be even more important than the time saved.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 13 '07 #23

This discussion thread is closed

Replies have been disabled for this discussion.