project06
Home ] Up ]

 

// file project05.cpp
// date 09/17/2007
// author AOU

#include <iostream>
#include <ctime>
using namespace std;

/*
Problem:
  Write a function that will find the highest two
  values out of the three values given

  given: x, y, z
  expected: a, b such that a and b are top two values of x, y, z
      and a>=b


Examples:
  Input (3,1,2) => (3,2)
  Input (1,1,2) => (2,1)
  Input (5,5,5) => (5,5)

Algorithm:
  input x,y,z 
  output a,b

  option1: use any sort to put x,y,z in increasing order, a=z,b=y
    if x>y then swap(x,y)
    if x>z then swap(x,z)
    if y>z then swap(y,z)
    a=z
    b=y

  option2: compare values 9x,y,z) and calculate a,b
    if x>=y and x>=z then 
        a=x
        if y>=z then b=y else b=z
      else if y>=x and y>=z then
        a=y
        if x>=z then b=x else b=z
      else 
        a=z
        if x>=y then b=x else b=y
      end if

  option3: use any sort to put x,y,z in increasing order, a=z,b=y
    do the following 2 times
      if x>y then swap(x,y)
      if y>z then swap(y,z)
      end do

    a=z
    b=y

*/

/////////////////////////////////////////////////////
//Assignment #1 9/17/2007 due 9/21/2007
//Prepare a 1-2 page summary of chapter 3 in the form
//  of a table.
/////////////////////////////////////////////////////


/////////////////////////////////////////////////////
//Assignment #2 9/17/2007 due 9/24/2007
//Implement the checkMax2 function
//Submit:
//  Printed source, results
/////////////////////////////////////////////////////

/*
Problem:
Develop a function that will make sure that a and b 
are the top two values from x,y,z and a>=b.

bool checkMax2(int x, int y, int z, int a, int b)

make sure [a, b] is a subset of [x, y, z]
make sure a >= b
make sure a is max of [x,y,z]
make sure b is max of [x,y,z]-[a]

Examples:
  (1,2,3,3,2) => true
  (1,2,3,3,1) => false
  (1,2,3,3,3) => false
  (1,2,3,4,3) => false
  (1,2,3,1,3) => false


Algorithm:
  make sure [a, b] is a subset of [x, y, z]:
    if a<>x and a<>y and a<>z then return false
    if b<>x and b<>y and b<>z then return false

  make sure a >= b:
    if b>a then return false
    
  make sure a is max of [x,y,z]:
    if a<x or a<y or a<z then return false

  make sure b is max of [x,y,z]-{v=a}:
    if a=x then swap(x,z)
    if a=y then swap(y,z)
    if b<x or b<y then return false
           
  return true

  end of algorithm
*/


/*
  Notes for the book:
    make sure b is max of [x,y,z]-[a]:

    plan1: put x,y,z in an array array[0:2], 
           delete the value from array[] that matches with a
           make sure b is highest of the remaining values in array[]

    plan2: if a=x then make sure b is highest of [y,z]
           if a=y then make sure b is highest of [x,z]
           if a=z then make sure b is highest of [x,y]

    plan3: if a=x then swap(x,z)
           if a=y then swap(y,z)
           if b<x or b<y then return false
           
*/

//prototypes of functions

void findMax2(int x, int y, int z, int &a, int &b);
void findMax2v2(int x, int y, int z, int &a, int &b);
void findMax2v3(int x, int y, int z, int &a, int &b);
void swap(int &p, int &q);
bool checkMax2(int x, int y, int z, int a, int b);

//prototypes of drivers

void driver_findMax(void);
void driver_checkMax2(void);


void main(void)
  {
  srand((unsigned int)time(NULL));

  //driver_findMax();

  driver_checkMax2();
  }


void findMax2(int x, int y, int z, int &a, int &b)
  // based on option3
  {
  for (int i=1; i<=2; i++)
    {
    if (x>y) swap(x,y);
    if (y>z) swap(y,z);
    }

  a=z;
  b=y;
  }


void swap(int &p, int &q)
  {
  int temp = p;
  p = q;
  q = temp;
  }


void findMax2v2(int x, int y, int z, int &a, int &b)
  {
  if (x>y) swap(x,y);
  if (x>z) swap(x,z);
  if (y>z) swap(y,z);
  a=z;
  b=y;
  }


void findMax2v3(int x, int y, int z, int &a, int &b)
  {
  a=911;
  b=911;
  if ((x>=y) && (x>=z))  
      {
      a=x;
      if (y>=z) b=y; else b=z;
      }
    else if ((y>=x) && (y>=z)) 
      {
      a=y;
      if (x>=z) b=x; else b=z;
      }
    else
      {
      a=z;
      if (x>=y) b=x; else b=y;
      }

  }


void driver_findMax(void)
  {
  cout << "--------------\n";
  cout << "driver_findMax\n";
  cout << "--------------\n";

  int x, y, z;
  int v1, v2;

  for (int i=1; i<=100; i++)
    {
    x = rand()%100;
    y = rand()%100;
    z = rand()%100;

    findMax2(x,y,z,v1,v2);
    cout << "(x, y, z)=" << "(" << x << ", " << y << ", " << z << ")\n";
    cout << "  (v1, v2) =" << "(" << v1 << ", " << v2 << ")\n";

    if (checkMax2(x,y,z,v1,v2))
      cout << "  results from findMax2 are correct\n";
    else
      cout << "  results from findMax2 are NOT correct\n";


    findMax2v2(x,y,z,v1,v2);
    cout << "  (v1, v2) =" << "(" << v1 << ", " << v2 << ")\n";

    if (checkMax2(x,y,z,v1,v2))
      cout << "  results from findMax2v2 are correct\n";
    else
      cout << "  results from findMax2v2 are NOT correct\n";

    findMax2v3(x,y,z,v1,v2);
    cout << "  (v1, v2) =" << "(" << v1 << ", " << v2 << ")\n";

    if (checkMax2(x,y,z,v1,v2))
      cout << "  results from findMax2v3 are correct\n";
    else
      cout << "  results from findMax2v3 are NOT correct\n";

    cout << endl;
    }
  }
  

/*
Problem:
  Develop a function that will make sure that a and b 
  are the top two values from x,y,z and a>=b.

make sure [a, b] is a subset of [x, y, z]
make sure a >= b
make sure a is max of [x,y,z]
make sure b is max of [x,y,z]-[a]


prototype:
  bool checkMax2(int x, int y, int z, int a, int b)


Examples:
  (1,2,3,3,2) => true
  (1,2,3,3,1) => false
  (1,2,3,3,3) => false
  (1,2,3,4,3) => false
  (1,2,3,1,3) => false


Algorithm:
  make sure [a, b] is a subset of [x, y, z]:
    if a<>x and a<>y and a<>z then return false
    if b<>x and b<>y and b<>z then return false

  make sure a >= b:
    if b>a then return false
    
  make sure a is max of [x,y,z]:
    if a<x or a<y or a<z then return false

  make sure b is max of [x,y,z]-{v=a}:
    if a=x then swap(x,z)
    if a=y then swap(y,z)
    if b<x or b<y then return false
           
  return true

  end of algorithm
*/

bool checkMax2(int x, int y, int z, int a, int b)
  {
  //supply the missing code
  return true;
  }


void driver_checkMax2(void)
  {
  cout << "----------------\n";
  cout << "driver_checkMax2\n";
  cout << "----------------\n";

  int TEST_DATA[12][5] = 
    {
      {1,2,3, 4,3},
      {1,2,3, 2,3},
      {1,2,3, 2,1},
      {1,2,3, 3,1},
      {1,2,3, 3,2},
      {1,1,1, 1,1},
      {5,3,1, 5,3},
      {2,8,7, 7,8},
      {5,6,9, 5,6},
      {1,8,0, 8,0},
      {3,2,9, 2,9},
      {-3,-2,0, 0, -2}
    };

  int r, c;
  int x,y,z, a,b;

  for (r=0; r<12; r++)
    {
    x=TEST_DATA[r][0];
    y=TEST_DATA[r][1];
    z=TEST_DATA[r][2];

    a=TEST_DATA[r][3];
    b=TEST_DATA[r][4];

    if (checkMax2(x,y,z,a,b))
      cout << "(" << a << "," << b << ") are max2 values of (" << x << "," << y << "," << z << ")\n";
    else
      cout << "(" << a << "," << b << ") are NOT max2 values of (" << x << "," << y << "," << z << ")\n";
    }

  }

/*
SAMPLE RUN OUTPUT:


*/