answersLogoWhite

0

How do you swap two numbers in c plus plus?

Updated: 8/17/2019
User Avatar

Wiki User

11y ago

Best Answer

void swap(int &x, int &y) { x ^= y ^= x;

}

- or -

void swap(int &x, int &y) {

int t = x;

x = y;

y = t;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap two numbers in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How to swap two numbers by call by reference in c plus plus?

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


How do you swap two numbers with bitwise operator in c plus plus?

// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Coding in c plus plus to swap two nos using pointer?

void swap (int &pa, int &pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }


What are the conditions to swap in c plus plus without temporary?

You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;


How do you add more than two numbers in C plus plus?

sum = a + b + c;


How do you swap two numbers in a C program?

Ellipses (...) used to emulate indentation... swap(int *a, int *b) { ... int temp; ... temp = *a; ... *a = *b; ... *b = temp; }


How do you swap two arrays in c plus plus using string?

You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness. The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first. A much better approach would be to point at the two arrays and swap the pointers instead.


Bubble sorting in c plus plus?

bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i<N-1; ++i) { if (A[i] > A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }


How do you Swap two numbers using bitwise operator C?

//lets assume a = 10; b = 20; a = a^b; b = a^b; a = a^b;


C plus plus programming for addition of two numbers?

int main() { int x = 40 + 2; }


C program to swapping two numbers using call by value method?

You have to pass the address of the variables.void swap (int *pa, int *pb){...}