how to do swapping???

Pages: 12
plz tell me how to do swapping i want to swap the value of b into a and value of a into b without the addition of variable ... plz help me fast i have to run it as soon as possible.
Last edited on
It is so easy.u can use XOR(^).
For example:

void swap(int &a, int &b)
{
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
}

This does not use temp variables.
The easiest way is to use std::swap.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <utility>

int main()
{
	char a = 'A';
	char b = 'B';
	
	std::swap(a, b);
	
	std::cout << "a = '" << a << "'\n";
	std::cout << "b = '" << b << "'\n";
}
a = 'B'
b = 'A'

http://www.cplusplus.com/reference/algorithm/swap/
thanks but plz explain in detail i have to run it.
u mean?
a=a+b;
b=a-b;
a=a-b;
It depends on the types of a and b. If for example they are std::string or double, then the XOR approach would not be appropriate.

std::swap is preferred - it may be optimised for particular types, rather than using a brute-force approach.
Last edited on
vector swapper<thing>;

swapper.push_back(a);
swapper.push_back(b);
a = swapper.pop_back();
b = swapper.pop_back();



jonnin, swapper is a variable.
@jonnin,

swapper is a variable, so technically your solution does not meet the requirements of the OP.

@OP,

Why the restriction of no added variable?

If you use std::swap, as suggested by others, that function is probably adding a new variable. So, you would probably be implicitly adding a variable even though the code you write does not do so.

Every new programmer learns that swapping the values of 2 variables is done with a 3rd temporary variable:

1
2
3
4
5
6
7
int a = 5;
int b = 7;
int temp;

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


Which brings us back to ... why the restriction?
bcz i have to run it without the addition of variable ...
thanx peter,chervil,jonnion,doug and killer.. thanx alott
Last edited on
i have to do ths without adding temp
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter two integer to swap\n");
scanf("%d%d,&a,&b");
a=a-b;
b=a-b;
a=a-b;
printf("a=%d\n=%d\n,a,b");
getch();
}
Last edited on
this is not running
champ195 wrote:
i have to do ths without adding temp

There are a few seemingly clever tricks that exchange the values of two (typically, integer) C-style variables using three reversible binary operations (xor, subtraction/addition, multiplication/division, etc). Whichever way you choose to solve your programming puzzle, make sure to never use that in real code: the proper C++ way, as already mentioned, is to use std::swap, the proper C way is to create a temp variable and use the assignment operator. Use of temp variable is faster, takes up less space, and works for more types and values than those three-step tricks.
thanx dear but my teacher told me to do this with out adding third variable... what can i do ??? above program also not running...
i am on first level i dont know too much about c programming
i am a teen ager
#include <stdio.h>
#include<conio.h>

void main()
{
clrscr();
int x = 10, y = 5;

x = x + y;
y = x -y;
x = x - y;

printf("After Swapping: x = %d, y = %d", x, y);

getch();
}
Last edited on
thanks friends i have done it by the above progrAme
Pages: 12