p02Max
Home ] Up ]

 

//p02Max
//Author: Me
//Date 07/03/2001

/*
Project#01
State the problem
Examples
Algorithms

Code: max2of3
      swap
      disp
      main
Output:10 cases

Date Assigned: July 3, 2001
Date Due:      July 6, 2001


*/
/*
Write a function that will return 
two highest values of the three integer values

name: max2of3

in :  a, b, c
out:  x, y

a, b, c, x, and y are integers
x should be >= y


Examples:
call max2of3(1,1,2,x,y)
 x=2, y=1

call max2of3(1,3,2,x,y)
 x=3, y=2

call max2of3(1,2,2,x,y)
 x=2, y=2

 
Plan1: Put the max of three in x, and max of the remaining in y

Plan2: Find the lowest, 
       put max of remaining in x,
       and the remaining of remaining in y

Plan3: Sort in decreasing order, x = a, y = b

Plan4: Put max in x, dump min, y gets the remaining

Plan5: m1 = max(a,b), m2 = max(b,c)
       x = max(m1, m2), y = min(m1, m2)
       (does not work)

Plan6: Dump min, put remaining ordered in x, y


Algorithm3:
Sort in decreasing order, x = a, y = b
-------
Sort a, b, c in decreasing order
x = a
y = b

-------
do the following 2 times
  if a<b then swap(a, b)
  if b<c then swap(b, c)
  end do

x = a
y = b
-------

*/

#include <iostream.h>

void max2of3(int a, int b, int c, int &x, int &y);
void swap(int &p, int &q);
void disp (int a, int b, int c, int x, int y);


void main (void)
  {
  int x, y;
  int a, b, c;

  a=1; b=2; c=3; x = 0; y = 0; max2of3(a, b, c, x, y); disp(a, b, c, x, y);
  a=3; b=2; c=1; x = 0; y = 0; max2of3(a, b, c, x, y); disp(a, b, c, x, y);
  a=1; b=3; c=2; x = 0; y = 0; max2of3(a, b, c, x, y); disp(a, b, c, x, y);

  a=1; b=2; c=2; x = 0; y = 0; max2of3(a, b, c, x, y); disp(a, b, c, x, y);
  a=2; b=2; c=2; x = 0; y = 0; max2of3(a, b, c, x, y); disp(a, b, c, x, y);
  a=3; b=2; c=2; x = 0; y = 0; max2of3(a, b, c, x, y); disp(a, b, c, x, y);
  }


void disp (int a, int b, int c, int x, int y)
  {
  cout << "(a, b, c) = (" << a << ", " << b << ", " << c << ")    "
       << "(x, y) = (" << x << ", " << y << ")\n";
  }


void swap(int &p, int &q)
  {
  if (p != q)
    {
    int t = p;
    p = q;
    q = t;
    }
  }


void max2of3(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 = a;
  y = b;
  }