prog03
Home ] Up ]

 

//prog03.cpp
//Author: Us
//Date:   01/25/2002


/*
Write a function to pick two highest integers 
from 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


*/

////////////////////////////////////////////////////////////
//includes
////////////////////////////////////////////////////////////
#include<iostream.h>


////////////////////////////////////////////////////////////
//prototypes											
////////////////////////////////////////////////////////////
void swap(int &v1, int &v2);
void max2(int x, int y, int z, int &a, int &b);
bool areMax2(int x, int y, int z, int a, int b);


////////////////////////////////////////////////////////////
//prototypes of test functions										
////////////////////////////////////////////////////////////
void testMax2(void);


////////////////////////////////////////////////////////////
//main
////////////////////////////////////////////////////////////
void main(void)
  {
  testMax2();
  }


////////////////////////////////////////////////////////////
//swap
//swaps values of two integer variables
////////////////////////////////////////////////////////////
//swap
void swap(int &v1, int &v2)
  {
  int temp;
  temp = v1;
  v1 = v2;
  v2 = temp;
  }


////////////////////////////////////////////////////////////
//max2
//finds two highest values
////////////////////////////////////////////////////////////
//max2
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;
  }


////////////////////////////////////////////////////////////
//testMax2
//tests max2 function
////////////////////////////////////////////////////////////
void testMax2(void)
  {
  int a=-1, b=-1;

  max2(1, 2, 3, a, b);
  cout << "max2(1, 2, 3, a, b) " << a << ' ' << b << endl;
  if(areMax2(1, 2, 3, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";

  max2(2, 1, 3, a, b);
  cout << "max2(2, 1, 3, a, b) " << a << ' ' << b << endl;
  if(areMax2(2, 1, 3, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";

  max2(3, 1, 2, a, b);
  cout << "max2(3, 1, 2, a, b) " << a << ' ' << b << endl;
  if(areMax2(3, 1, 2, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";
  
  max2(1, 3, 2, a, b);
  cout << "max2(1, 3, 2, a, b) " << a << ' ' << b << endl;
  if(areMax2(1, 3, 2, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";
  
  max2(3, 2, 1, a, b);
  cout << "max2(3, 2, 1, a, b) " << a << ' ' << b << endl;
  if(areMax2(3, 2, 1, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";

  max2(3, 3, 3, a, b);
  cout << "max2(3, 3, 3, a, b) " << a << ' ' << b << endl;
  if(areMax2(3, 3, 3, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";

  if(areMax2(1, 2, 3, a, b))
      cout << "WORKING\n";
    else
      cout << "NOT WORKING\n";

  }


////////////////////////////////////////////////////////////
//areMax2
//checks if the values are two highest
////////////////////////////////////////////////////////////
//areMax2
/*
Given:  x, y, z, a, b
Return: true or false
Logic:  return true if
          a, b are 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
*/
bool areMax2(int x, int y, int z, int a, int b)
  {
  if (!(a >= b)) return false;
  if (!((a>=x)&&(a>=y)&&(a>=z))) return false;
  if ((a==z) && (!((b>=x)&&(b>=y)))) return false;
  if ((a==x) && (!((b>=y)&&(b>=z)))) return false;
  if ((a==y) && (!((b>=z)&&(b>=x)))) return false;
  return true;
  }