473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

SWAP VALUE

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
22 2021
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dooza | last post by:
Hi there, I was wondering if anyone had come across some javascript that would allow me to have a chart of say 20 music tracks, and be able to move each track up and down the chart using up/down...
14
by: Otto Meijer | last post by:
Hi everyone, for one of my projects I need to figure out the size of the swap file(s) of a certain system. The problem is I need to do this on a host of platforms like: HP_UX, Solaris, Linux,...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
9
by: Jongmin Lee | last post by:
Hi Everybody, I have very simple code snippet to explain my problem. Class "Swap" is construncted in "Main" with two initial variables. Later, "Swap" class is going to swap those two...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
3
by: Narmada Padhy | last post by:
1> #include<iostream> using namespace std; void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;
10
by: Chad | last post by:
I hope this question doesn't appear twice. I tried posting it once, however, I got a stupid 505 internal error on google. When this happens, the post will either post 30 minutes later or won't post...
21
by: raylopez99 | last post by:
In the otherwise excellent book C# 3.0 in a Nutshell by Albahari et al. (3rd edition) (highly recommended--it's packed with information, and is a desktop reference book) the following statement is...
2
by: CisFun | last post by:
Ok, so I've successfully written my swap application but now i'm trying to rewrite it so i'm using pointers to pass the address of the argument to the function. I'm going to have the swapping...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.