how to do swapping???

Pages: 12
Aug 14, 2017 at 11:01am
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 Aug 14, 2017 at 11:02am
Aug 14, 2017 at 11:38am
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.
Aug 14, 2017 at 11:44am
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/
Aug 14, 2017 at 11:45am
thanks but plz explain in detail i have to run it.
Aug 14, 2017 at 11:46am
u mean?
a=a+b;
b=a-b;
a=a-b;
Aug 14, 2017 at 11:50am
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 Aug 14, 2017 at 11:53am
Aug 14, 2017 at 1:15pm
vector swapper<thing>;

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



Aug 14, 2017 at 1:36pm
jonnin, swapper is a variable.
Aug 14, 2017 at 1:38pm
@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?
Aug 14, 2017 at 2:45pm
bcz i have to run it without the addition of variable ...
Aug 14, 2017 at 2:46pm
thanx peter,chervil,jonnion,doug and killer.. thanx alott
Last edited on Aug 14, 2017 at 2:46pm
Aug 14, 2017 at 2:48pm
i have to do ths without adding temp
Aug 14, 2017 at 3:18pm
#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 Aug 14, 2017 at 3:35pm
Aug 14, 2017 at 3:19pm
this is not running
Aug 14, 2017 at 3:21pm
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.
Aug 14, 2017 at 3:30pm
thanx dear but my teacher told me to do this with out adding third variable... what can i do ??? above program also not running...
Aug 14, 2017 at 3:31pm
i am on first level i dont know too much about c programming
Aug 14, 2017 at 3:32pm
i am a teen ager
Aug 14, 2017 at 3:58pm
#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 Aug 14, 2017 at 3:59pm
Aug 14, 2017 at 3:59pm
thanks friends i have done it by the above progrAme
Pages: 12