You can swap two variables, by storing one of them temporarily in a third variable, like this:
temp = a;
a = b;
b = temp;
Inside a function, this won't work, because the function parameters are COPIES of the original variables, not the variables themselves. Any change won't affect the original variables. If you work with OBJECTS, and swap the CONTENTS of the objects (not the object pointers), it can work, though.
a=a+b; b=a-b; a=a-b;
using pointers, example: void Swapd (double *d1, double *d2) { . double tmp= *d1; . *d1= *d2; . *d2= *tmp; }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);
a += b; b -= a; a -= b;
#include<iostream> using namespace std; void swap(int &i, int &j) { int temp = i; i = j; j = temp; } int main() { int i,j; cin>>i>>j; cout << i << j << endl; swap(i,j); cout << i << j; return 0; }
By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;
temp = a; a = b; b = temp;temp = a; a = b; b = temp;temp = a; a = b; b = temp;temp = a; a = b; b = temp;
The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int& p, int& q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }
a=a^b; b=a^b; a=a^b;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)