|
//file: p001.cpp //Author: AOU #include <iostream.h> //Problem: A function to give top two values of the three // top2of3(x, y, z, t1, t2) //Example: top2of3(1, 2, 3, t1, t2) => t1=3, t2=2 //Algorithm: /* Algorithm0 put x, y, z in decreasing order t1 = z t2 = y Algorithm1 if z<x swap z and x if z<y swap z and y if y<x then swap x and y t1 = z t2 = y */ void swap(int &x, int &y) { int temp; temp = x; x = y; y = temp; } void top2of3(int x, int y, int z, int &t1, int &t2) { if (z<x) swap(z, x); if (z<y) swap(z, y); if (y<x) swap(x, y); t1 = z; t2 = y; } void main(void) { int x=1, y=2, z=3, t1, t2; top2of3(x, y, z, t1, t2); cout << t1 << ' ' << t2 << endl; } |