|
// prog002b.cpp // Author: me // 01/22/2001 // Implemented two more functions // 01/19/2001 // Implemented two functions // 01/17/2001 #include <iostream.h> void swap(int &p, int &q) { if (p != q) { int t = p; p = q; q = t; } } /* Write a function that will give you two highest values of the three integer values. Examples: Input: 1, 2, 3 Output: 2, 3 Input: 2, 2, 3 Output: 2, 3 Input: 1, 1, 1 Output: 1, 1 Input: a, b, c Output: x, y so that x and y are the highest values /////////////////////////////////////////////////////////// //Plans /////////////////////////////////////////////////////////// //Plan1: Find max of three, find max of remaining //Plan2: Sort and pick the last two //Plan3: if a is max then max of b and c ..... //Plan4: Find minimum and drop it /////////////////////////////////////////////////////////// //Plan1: Find max of three, find max of remaining /////////////////////////////////////////////////////////// swap the highest of a, b, and c with c swap the highest of a and b with b x = c y = b /////////////////////////////////////////////////////////// //Plan2: Sort and pick the last two /////////////////////////////////////////////////////////// Algorithm: do the following 2 times: if a > b then swap a, b if b > c then swap b, c end do x = c y = b /////////////////////////////////////////////////////////// //Plan3: if a is max then max of b and c ..... /////////////////////////////////////////////////////////// Algorithm: if a>=b and a>=c then x = a if b>c then y = b else y = c else if b>=c and b>=a then x = b if a>c then y = a else y = c else x = c if a>b then y = a else y = b end if /////////////////////////////////////////////////////////// //Plan4: Find minimum and drop it /////////////////////////////////////////////////////////// Algorithm: if a<=b and a<=c then x = b, y = c else if b<=c and b<=a then x = a, y = c else x = a, y = b end if */ /////////////////////////////////////////////////////////// //Plan1: Find max of three, find max of remaining /////////////////////////////////////////////////////////// /* swap the highest of a, b, and c with c swap the highest of a and b with b x = c y = b */ void max2of3v1(int a, int b, int c, int &x, int &y) { if (a >= b && a >= c) //Is a is maximum? swap (a, c); else if (b >= a && b >= c) //Is b is maximum? swap (b, c); if (a > b) swap (a, b); x = c; y = b; } /////////////////////////////////////////////////////////// //void max2of3v2(int a, int b, int c, int &x, int &y) /////////////////////////////////////////////////////////// //Plan2: Sort and pick the last two /////////////////////////////////////////////////////////// /* Algorithm: do the following 2 times: if a > b then swap a, b if b > c then swap b, c end do x = c y = b */ void max2of3v2(int a, int b, int c, int &x, int &y) { for (int i=1; i<= 2; i++) { if (a > b) swap(a, b); if (b > c) swap(b, c); } x = c; y = b; } /////////////////////////////////////////////////////////// //void max2of3v4(int a, int b, int c, int &x, int &y) /////////////////////////////////////////////////////////// /* Plan4: Find minimum and drop it Algorithm: if a<=b and a<=c then x = b, y = c else if b<=c and b<=a then x = a, y = c else x = a, y = b end if */ void max2of3v4(int a, int b, int c, int &x, int &y) { if (a<=b && a<=c) x = b, y = c; else if (b<=c && b<=a) x = a, y = c; else x = a, y = b; } /////////////////////////////////////////////////////////// //void max2of3v3(int a, int b, int c, int &x, int &y) /////////////////////////////////////////////////////////// /* Plan3: if a is max then max of b and c ..... Algorithm: if a>=b and a>=c then x = a if b>c then y = b else y = c else if b>=c and b>=a then x = b if a>c then y = a else y = c else x = c if a>b then y = a else y = b end if */ void max2of3v3(int a, int b, int c, int &x, int &y) { if (a>=b && a>=c) { x = a; if (b > c) y = b; else y = c; } else if (b >= c && b >= a) { x = b; if (a > c) y = a; else y = c; } else { x = c; if (a > b) y = a; else y = b; } } /////////////////////////////////////////////////////////// //main /////////////////////////////////////////////////////////// void main(void) { int i, j, k, m1, m2; for (i=1; i<=3; i++) for (j=1; j<=3; j++) for (k=1; k<=3; k++) { cout << i << ' ' << j << ' ' << k << ' '; max2of3v1(i, j, k, m1, m2); //max2of3v2(i, j, k, m1, m2); //max2of3v3(i, j, k, m1, m2); //max2of3v4(i, j, k, m1, m2); cout << "max2 are =" << m1 << ' ' << m2 << endl; } } |