Class Examples 7/15/02 Example #1 ---------- #include #include void swap(double &a, double &b); int main() { double x, y; cout << "Enter two numbers.\n"; cin >> x >> y; if (x>y) swap(x,y); cout << "Your numbers in order are " << x << " and " << y << endl; } void swap(double &a, double &b) { double temp = a; a = b; b = temp; } Example #2 ---------- #include #include void swap(double &a, double &b); void sort(double &a,double &b,double &c); int main() { double x, y, z; cout << "Enter three numbers\n"; cin >> x >> y >> z; sort(x,y,z); cout << "Your sorted numbers are : "; cout << x << " " << y << " " << z << endl; } void sort(double &a, double &b, double&c) { if (a>b) swap(a,b); if (b>c) swap(b,c); if (a>b) swap(a,b); } Example #3 ---------- #include int f(int& a); void main() { int i = 1; int j = 2; if ((f(j)>1) || (i<=3)) //LINE 9 j = j + 1; cout << i << " is i.\n"; cout << j << " is j.\n"; } int f(int& a) { a = 1000; return 50; } Example #4 ---------- #include int f1(int a, int &b); void f2(int &x, int &y); void main() { int x=5, y=2; y = f1(y, x); cout << "x = " << x << " y = " << y << endl; f2(x,y); cout << "x = " << x << " y = " << y << endl; } int f1(int a, int &b) { int x=2, y; y = a+b; b = x*y; a = 15-b; x++; cout << "a = " << a << " b = " << b << " x = " << x << " y = " << y << endl; return b-a; } void f2(int &x, int &y) { int a=1, b=2; x = a*b*y; y = a+b+x; a = 3*b+5; if (a > y) b++; cout << "a = " << a << " b = " << b << " x = " << x << " y = " << y << endl; }