// Swap
// Demonstrates passing references to alter argument variables
#include
<iostream>
using
namespace std;
void
badSwap(int x, int y);
void
goodSwap(int& x, int& y);
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& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
swap(&grades[num],&grades[num+1]); what it make in a program?
int a,b; a=a+b; b=a-b; a=a-b; that's it simple
swap (int *pa, int *pb) { *pa ^= *pb; *pa ^= *pa; *pa ^= *pb; }
#include<iostream> void swap(int* x, int* x){ (*x)^=(*y)^=(*x)^=(*y); } int main() { int a, b; a=10; b=20; std::cout<<"Before swap: a="<<a<<", b="<<b<<std::endl; swap(&a,&b); std::cout<<"After swap: a="<<a<<", b="<<b<<std::endl; return(0); }
void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
Ellipses (...) used to emulate indentation... swap(int *a, int *b) { ... int temp; ... temp = *a; ... *a = *b; ... *b = temp; }
swap(&grades[num],&grades[num+1]); what it make in a program?
#include<conio.h> main() { int a,b; scanf("%d%d",&a,&b); printf("Before swapping A= %d B=%d",a,b); swap(a,b); getch(); } void swap(int a,int b) { int c; c=a; a=b; b=c; printf("After swapping A= %d B=%d",a,b); }
a=a^b; b=a^b; a=a^b;
#include using namespace std; void swap(int &a, int &b); int main() { int x=5,y=3; cout
You have to pass the address of the variables.void swap (int *pa, int *pb){...}
int a,b; a=a+b; b=a-b; a=a-b; that's it simple
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }
void main() { int a=2,b=5; b=a+b; a=b-a; b=b-a; getch(); }
Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;
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.