|
//prog02.cpp //Author: Us //Date: 01/18/2002 /* Write a function to pick two highest integers of three integers given Given: 1, 2, 3 Return: 3, 2 Given: x, y, z Return: a, b such that a >= b a >= x, y, and z b >= x and y if a= z b >= y and z if a= x b >= z and x if a= y Name: max2(x, y, z, a, b) Algorithm0: Get the highest value in a Get the next highest in b Algorithm1v0: Sort x, y, z in dec order a = x b = y Algorithm1v1: Sort x, y, z in dec order if (x < y) swap(x, y) if (x < z) swap(x, z) if (y < z) swap(y, z) a = x b = y */ #include<iostream.h> void swap(int &v1, int &v2); void max2(int x, int y, int z, int &a, int &b); void main(void) { int a=-1, b=-1; max2(1, 2, 3, a, b); cout << "max2(1, 2, 3, a, b) " << a << ' ' << b << endl; max2(2, 1, 3, a, b); cout << "max2(2, 1, 3, a, b) " << a << ' ' << b << endl; max2(3, 1, 2, a, b); cout << "max2(3, 1, 2, a, b) " << a << ' ' << b << endl; max2(1, 3, 2, a, b); cout << "max2(1, 3, 2, a, b) " << a << ' ' << b << endl; max2(3, 2, 1, a, b); cout << "max2(3, 2, 1, a, b) " << a << ' ' << b << endl; max2(3, 3, 3, a, b); cout << "max2(3, 3, 3, a, b) " << a << ' ' << b << endl; } void swap(int &v1, int &v2) { int temp; temp = v1; v1 = v2; v2 = temp; } void max2(int x, int y, int z, int &a, int &b) { //Sort x, y, z in dec order if (x < y) swap(x, y); if (x < z) swap(x, z); if (y < z) swap(y, z); a = x; b = y; } |