| Two versions of swap - with references and with pointers. | Lecture 2 - slide 19 : 42 Program 1 |
#include <iostream>
using namespace std;
void swap_doubles_by_ref(double &d1, double &d2){
double temp;
temp = d1;
d1 = d2;
d2 = temp;
}
void swap_doubles_by_ptr(double *d1, double *d2){
double temp;
temp = *d1;
*d1 = *d2;
*d2 = temp;
}
int main(){
double e, f;
e = 5.0, f = 7.0;
cout << e << " " << f << endl; // 5 7
swap_doubles_by_ref(e, f);
cout << e << " " << f << endl; // 7 5
e = 5.0, f = 7.0;
cout << e << " " << f << endl; // 5 7
swap_doubles_by_ptr(&e, &f);
cout << e << " " << f << endl; // 7 5
}