SWAP VALUE

P

Pramod

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
 
R

Ron Natalie

Pramod said:
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.
 
P

Pramod

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.
 
D

Devon Null

Pramod said:
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.
 
P

Pramod

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
 
R

Richard Herring

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.
 
P

Puppet_Sock

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
 
S

SimpleCode

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;
}
 
J

jjds101

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 /");
}
 
D

Devon Null

Pramod said:
Hello Devon
I am Guy from INDIA..so u will give me code

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.
 
D

Daniel Kraft

Pramod said:
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
 
J

James Kanze

Pramod wrote:
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?
 
P

Pramod

Which almost certainly uses the first method, because it is the
only reliable method (but I know you know that).


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:[email protected]
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.
 
P

Pramod

In message <[email protected]>,







Hello Guy.


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 its not a threat....what's its mean 'u' "you" u have no manners
to talk any unknown people
 
R

Richard Herring


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.
 
R

Ron Natalie

James said:
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.
 
R

Ron Natalie

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.
 
L

Lionel B

[...]
[...]


[...]
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".


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top