// Swap Pointer
// Demonstrates passing constant pointers to alter argument variables
#include
<iostream>
using
namespace std;
void
badSwap(int x, int y);
void
goodSwap(int* const pX, int* const pY);
int
main()
{
int myScore = 150;
int yourScore = 1000;
cout <<
"Original values\n";
cout <<
"myScore: " << myScore << "\n";
cout <<
"yourScore: " << yourScore << "\n\n";
cout <<
"Calling badSwap()\n";
badSwap(myScore, yourScore);
cout <<
"myScore: " << myScore << "\n";
cout <<
"yourScore: " << yourScore << "\n\n";
cout <<
"Calling goodSwap()\n";
goodSwap(&myScore, &yourScore);
cout <<
"myScore: " << myScore << "\n";
cout <<
"yourScore: " << yourScore << "\n";
return 0;
}
void
badSwap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void
goodSwap(int* const pX, int* const pY)
{
//store value pointed to by pX in temp
int temp = *pX;
//store value pointed to by pY in address pointed to by pX
*
pX = *pY;
//store value originally pointed to by pX in address pointed to by pY
*
pY = temp;
}
You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;
t = a; a = b; b = t; // t is a third integer variable (swap variable) But here's a way without a swap variable, given as as a macro in C: #define SWAP(a,b) { if (a!=b) { a^=b; b^=a; a^=b; }} // Swap macro by XOR Once you define it, you can say swap(x,y) to swap x and y. The numbers kind of flow through each other and end up swapped.
#include<iostream> void swap(int* x, int* y) { int tmp = *x; *x=*y; *y=tmp; } int main() { int a=2, b=4; std::cout<<"a="<<a<<", b="<<b<<std::endl; swap(&a, &b); std::cout<<"a="<<a<<", b="<<b<<std::endl; }
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; } int c = 13; int d = 27; swap (&c, &d); /*c is now 27 and d is now 13 */ Note: there is no call-by-reference in C. In C++: void swap (int &a, int &b) { . int tmp; . tmp = a; . a = b; . b = tmp; }
C: there are no methods in C. C++: no.
a pointer that is not pointing to anything
void swap(int& a, int& b ) { a^=b^=a^=b; }
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]; } } } }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
void swap (int &pa, int &pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }
You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;
It is not a reserved word, so can be an identifier (name of a type/variable/function).
void swap(int &x, int &y) { x ^= y ^= x; } - or - void swap(int &x, int &y) { int t = x; x = y; y = t; }
// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b
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.
void sort (int& a, int& b, int& c) { if (a>b) std::swap (a, b); if (b>c) std::swap (b, c); else return; if (a>b) std::swap (a, b); } Note that this is based upon a bubble sort algorithm. Although usually inefficient as a general sorting algorithm, given that we know there are only three elements means we can implement it reasonably efficiently without any additional space complexity. There will always be 2 or 3 comparisons but at most there will be 3 swaps. The only improvement we could really make is to implement a type of selection sort: void sort (int& a, int& b, int& c) { if (a>b && a>c) std::swap (a, c); else if (b>c) std::swap (b, c); if (a>b) std::swap (a, b); } Here we either make 3 or 4 comparisons but only 2 swaps at most. The assumption here is that a comparison is a quicker operation than a swap thus the selection sort method is more efficient. However, unless you were to sort millions of sets of three one after the other, you are unlikely to see any measurable difference in performance.
a/c filter with an arrow, is is placed in the unit with the arrow pointing toward the a/c fan??