|
// prog002d.cpp // Author: me // 01/26/2001 Test function // 01/24/2001 Reorganized and added new test scheme // 01/22/2001 Implemented two more functions // 01/19/2001 Implemented two function // 01/17/2001 Started /* Write functions that will give you two highest values of the three integer values given. */ #include <iostream.h> #include <math.h> #include <stdlib.h> #include <time.h> /////////////////////////////////////////////////////////// //prototypes /////////////////////////////////////////////////////////// void swap(int &p, int &q); void max2of3v1(int a, int b, int c, int &x, int &y); void max2of3v2(int a, int b, int c, int &x, int &y); void max2of3v3(int a, int b, int c, int &x, int &y); void max2of3v4(int a, int b, int c, int &x, int &y); bool inspectorGadget(int a, int b, int c, int x, int y); /////////////////////////////////////////////////////////// //main /////////////////////////////////////////////////////////// void main(void) { int i, j, k, m1, m2; char ch; srand(time(NULL)); /* 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; } */ do { i = rand() % 100; j = rand() % 100; k = rand() % 100; cout << i << ' ' << j << ' ' << k << endl; max2of3v1(i, j, k, m1, m2); cout << "max2v1 are =" << m1 << ' ' << m2 << endl; if(inspectorGadget(i, j, k, m1, m2)) cout << "Okay\n"; else cout << "NOT Okay\n"; max2of3v2(i, j, k, m1, m2); cout << "max2v2 are =" << m1 << ' ' << m2 << endl; if(inspectorGadget(i, j, k, m1, m2)) cout << "Okay\n"; else cout << "NOT Okay\n"; max2of3v3(i, j, k, m1, m2); cout << "max2v3 are =" << m1 << ' ' << m2 << endl; if(inspectorGadget(i, j, k, m1, m2)) cout << "Okay\n"; else cout << "NOT Okay\n"; max2of3v4(i, j, k, m1, m2); cout << "max2v4 are =" << m1 << ' ' << m2 << endl; if(inspectorGadget(i, j, k, m1, m2)) cout << "Okay\n"; else cout << "NOT Okay\n"; cout << "Enter 0 to quit "; cin >> ch; } while (ch != '0'); } /////////////////////////////////////////////////////////// //bool inspectorGadget(int a, int b, int c, int x, int y); /////////////////////////////////////////////////////////// bool inspectorGadget(int a, int b, int c, int x, int y) { //check if x and y are the top values of a, b, c //sort a, b, and c in increasing order //check if x == c and y = b for (int i=1; i<=2; i++) { if (a > b) swap(a, b); if (b > c) swap(b, c); } //check if x and y are the top values of a, b, c if (x != c) return false; if (y != b) return false; //check if x is higher than y if (y > x) return false; //check for a foreign value if ((x != a) && (x != b) && (x != c)) return false; if ((y != a) && (y != b) && (y != c)) return false; return true; } /////////////////////////////////////////////////////////// //swap /////////////////////////////////////////////////////////// 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 make sure x and y are in decreasing order */ 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; if (x < y) swap(x, y); } /////////////////////////////////////////////////////////// //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; } } |