// prog002a.cpp
// Author: Kailash Chandra
// 01/19/2001
// Implemented two functions
// 01/17/2001
#include <iostream.h>
/*
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
///////////////////////////////////////////////////////////
//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
*/
///////////////////////////////////////////////////////////
//void max2of3(int a, int b, int c, int &x, int &y)
///////////////////////////////////////////////////////////
/*
oPlan4: 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
*/
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;
}
///////////////////////////////////////////////////////////
//void max2of3v3(int a, int b, int c, int &x, int &y)
///////////////////////////////////////////////////////////
/*
oPlan3: 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;
}
}
///////////////////////////////////////////////////////////
//main
///////////////////////////////////////////////////////////
void main(void)
{
int i, j, k, m1, m2;
for (i=1; i<=3; i++)
for (j=1; j<=3; j++)
for (k=1; k<=3; k++)
{
cout << i << ' ' << j << ' ' << k << ' ';
max2of3v3(i, j, k, m1, m2);
//max2of3v4(i, j, k, m1, m2);
cout << "max2 are =" << m1 << ' ' << m2 << endl;
}
}
|