1,105,380 Community Members

Swap two numbers

Member Avatar
jodysmith
Newbie Poster
5 posts since Jan 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-6
 

Hello,
I have just a simple and basic question. Write a program in c++ to swap 2 numbers without using a third variable.
:D

Member Avatar
adnan.siddique
Light Poster
31 posts since Jan 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,
I have just a simple and basic question. Write a program in c++ to swap 2 numbers without using a third variable.
:D

just focus on what u can do and what u can not.

u can not use a third variable.
wat u can do is "4 operations of + _ * and /". by using these operations u have to do it.
a hint is

if a = 5 and b = 9
u have to make b = 5 and a = 9.
to make b = 5 u have to subtract 4 from 9. but where wil that 4 comes from? now its your turn.................

Member Avatar
jodysmith
Newbie Poster
5 posts since Jan 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-2
 

just focus on what u can do and what u can not.

u can not use a third variable.
wat u can do is "4 operations of + _ * and /". by using these operations u have to do it.
a hint is

if a = 5 and b = 9
u have to make b = 5 and a = 9.
to make b = 5 u have to subtract 4 from 9. but where wil that 4 comes from? now its your turn.................

Ok fine, here is the solution:-
suppose a=10, b=5
a=a+b; //a=15
b=a-b; //b=10
a=a-b; //a=5
It needs only 3 steps .

Member Avatar
adnan.siddique
Light Poster
31 posts since Jan 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok fine, here is the solution:-
suppose a=10, b=5
a=a+b; //a=15
b=a-b; //b=10
a=a-b; //a=5
It needs only 3 steps .

You should not tell him the solution. That is wrong

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

You should not tell him the solution. That is wrong

I'm guilty of the mistake too, but that is the OP posting what they came up with after your comments. Their wording could have been much less snooty though.

Member Avatar
Vinodvm
Newbie Poster
1 post since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-3
 

The following code will do it...

#include<iostream>
using namespace std;
int main()
{
	int a=5,b=10;
    cout << "a: " << a << " b: " << b <<endl;
	a=a+b;
	b=a-b;  //this makes b=a
	a=a-b;  //this makes a=b
	cout << "a: " << a << " b: " << b <<endl;
}
Member Avatar
adnan.siddique
Light Poster
31 posts since Jan 2010
Reputation Points: -6 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

sir please put code tags

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
6
 

> Write a program in c++ to swap 2 numbers without using a third variable.
The short answer is you can't, at least not in any way that is remotely useful.

For those suggesting addition, try this

#include <stdio.h>
#include <limits.h>

void f1 ( void ) {
  double a = 1E10, b = 1E-10;
  printf( "a=%.10f, b=%.10f\n", a, b );
  a = a + b;
  b = a - b;
  a = a - b;
  printf( "a=%.10f, b=%.10f\n", a, b );
  printf( "Oh dear, the tiny value got lost next to the huge value\n\n" );
}
void f2 ( void ) {
  int a = INT_MAX, b = INT_MAX - 1;
  printf( "%d %d\n", a, b );
  a = a + b;
  printf( "a is now %d - overflow\n", a );
  b = a - b;
  a = a - b;
  printf( "%d %d\n", a, b );
}

int main ( ) {
  f1();
  f2();
  return 0;
}

$ gcc foo.c
$ ./a.out 
a=10000000000.0000000000, b=0.0000000001
a=0.0000000000, b=10000000000.0000000000
Oh dear, the tiny value got lost next to the huge value

2147483647 2147483646
a is now -3 - overflow
2147483646 2147483647

So what you say, at least int eventually gave the right answer.
True, but only by luck rather than skill.

H.2.2 Integer types
1 The signed C integer types int, long int, long long int, and the corresponding
unsigned types are compatible with LIA−1. If an implementation adds support for the
LIA−1 exceptional values integer_overflow and undefined, then those types are LIA−1
conformant types. C’s unsigned integer types are ‘‘modulo’’ in the LIA−1 sense in that
overflows or out-of-bounds results silently wrap. An implementation that defines signed
integer types as also being modulo need not detect integer overflow, in which case, only
integer divide-by-zero need be detected.

The implementation is within its rights to cause a trap on integer overflow. So rather than swapping two variables, your machine is instead rebooting.

You might want to ask yourself why your tutor is teaching you stupid assembler programming tricks from the 1970's.

Here's why you should write this:

temp = a;
a = b;
b = temp;

- it ALWAYS works, irrespective to the type. Yes, you can even use it on structs or classes if you want. Try that with your dumb math tricks.
- there is no danger of a "loss of precision" (good for swapping floats).
- there is no danger of an arithmetic overflow (good for integers of all types)
- the compiler probably going to see it as a "swap", and may end up doing some thing you didn't think of doing. For example, generating an actual "exch" instruction. The obfuscated code is just as likely to confuse a compiler as it is to confuse the average reader.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article