project03
Home ] Up ]

 

// file project03.cpp
// date 09/10/2007
// author AOU

#include <iostream>
using namespace std;

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

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

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

  option1: 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

*/

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

void main(void)
  {
  int v1, v2;
  findMax2(1,2,3,v1,v2);
  cout << v1 << endl;
  cout << v2 << endl;
  }

void findMax2(int x, int y, int z, int &a, int &b)
  {
  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;
  }