//prog05.cpp
//Author: Us
//Date: 01/30/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>
#include<stdlib.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);
bool areMax2a(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)
{
if (v1 != v2)
{
int 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);
//copy x to a and y to b
a = x;
b = y;
}
////////////////////////////////////////////////////////////
//testMax2
//tests max2 function
////////////////////////////////////////////////////////////
void testMax2(void)
{
int v1, v2, v3, a, b;
for (int i=1; i<=40; i++)
{
v1 = rand()%10;
v2 = rand()%10;
v3 = rand()%10;
max2(v1, v2, v3, a, b);
cout << "max2(" << v1 << ", " << v2 << ", "
<< v3 << ", " << a << ", " << b << "); ";
if(areMax2(v1, v2, v3, a, b))
cout << "PASSED ";
else
cout << "FAILED ";
if(areMax2a(v1, v2, v3, a, b))
cout << "PASSED\n";
else
cout << "FAILED\n";
a = rand()%10;
b = rand()%10;
cout << "max2(" << v1 << ", " << v2 << ", "
<< v3 << ", " << a << ", " << b << "); ";
if(areMax2(v1, v2, v3, a, b))
cout << "PASSED ";
else
cout << "FAILED ";
if(areMax2a(v1, v2, v3, a, b))
cout << "PASSED\n";
else
cout << "FAILED\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
a should be one of x, y, z
b should be one of remaining of x, y, z
else return false
*/
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;
if ((a!=x) && (a!=y) && (a!=z)) return false;
if ((a == x) && ((b!=y) && (b!=z))) return false;
if ((a == y) && ((b!=x) && (b!=z))) return false;
if ((a == z) && ((b!=x) && (b!=y))) return false;
return true;
}
////////////////////////////////////////////////////////////
//areMax2a
//checks if the values are two highest
////////////////////////////////////////////////////////////
//areMax2a
/*
sort x, y, z
put z in ta, and y in tb
do the following 2 times
if x>y then swap x, y
if y>z then swap y, z
ta=z, tb=y
make sure that ta is a and tb is b
*/
bool areMax2a(int x, int y, int z, int a, int b)
{
int ta, tb;
for (int i=1; i<=2; i++)
{
if (x>y) swap (x, y);
if (y>z) swap (y, z);
}
ta=z, tb=y;
return ((ta == a) && (tb == b));
}
/*
SAMPLE RUN
max2(1, 7, 4, 7, 4); PASSED PASSED
max2(1, 7, 4, 0, 9); FAILED FAILED
max2(4, 8, 8, 8, 8); PASSED PASSED
max2(4, 8, 8, 2, 4); FAILED FAILED
max2(5, 5, 1, 5, 5); PASSED PASSED
max2(5, 5, 1, 7, 1); FAILED FAILED
max2(1, 5, 2, 5, 2); PASSED PASSED
max2(1, 5, 2, 7, 6); FAILED FAILED
max2(1, 4, 2, 4, 2); PASSED PASSED
max2(1, 4, 2, 3, 2); FAILED FAILED
max2(2, 1, 6, 6, 2); PASSED PASSED
max2(2, 1, 6, 8, 5); FAILED FAILED
max2(7, 6, 1, 7, 6); PASSED PASSED
max2(7, 6, 1, 8, 9); FAILED FAILED
max2(2, 7, 9, 9, 7); PASSED PASSED
max2(2, 7, 9, 5, 4); FAILED FAILED
max2(3, 1, 2, 3, 2); PASSED PASSED
max2(3, 1, 2, 3, 3); FAILED FAILED
max2(4, 1, 1, 4, 1); PASSED PASSED
max2(4, 1, 1, 3, 8); FAILED FAILED
max2(7, 4, 2, 7, 4); PASSED PASSED
max2(7, 4, 2, 7, 7); FAILED FAILED
max2(9, 3, 1, 9, 3); PASSED PASSED
max2(9, 3, 1, 9, 8); FAILED FAILED
max2(6, 5, 0, 6, 5); PASSED PASSED
max2(6, 5, 0, 2, 8); FAILED FAILED
max2(6, 0, 2, 6, 2); PASSED PASSED
max2(6, 0, 2, 4, 8); FAILED FAILED
max2(6, 5, 0, 6, 5); PASSED PASSED
max2(6, 5, 0, 9, 0); FAILED FAILED
max2(0, 6, 1, 6, 1); PASSED PASSED
max2(0, 6, 1, 3, 8); FAILED FAILED
max2(9, 3, 4, 9, 4); PASSED PASSED
max2(9, 3, 4, 4, 6); FAILED FAILED
max2(0, 6, 6, 6, 6); PASSED PASSED
max2(0, 6, 6, 1, 8); FAILED FAILED
max2(4, 9, 6, 9, 6); PASSED PASSED
max2(4, 9, 6, 3, 7); FAILED FAILED
max2(8, 8, 2, 8, 8); PASSED PASSED
max2(8, 8, 2, 9, 1); FAILED FAILED
max2(3, 5, 9, 9, 5); PASSED PASSED
max2(3, 5, 9, 8, 4); FAILED FAILED
max2(0, 7, 6, 7, 6); PASSED PASSED
max2(0, 7, 6, 3, 6); FAILED FAILED
max2(1, 5, 4, 5, 4); PASSED PASSED
max2(1, 5, 4, 2, 0); FAILED FAILED
max2(9, 7, 3, 9, 7); PASSED PASSED
max2(9, 7, 3, 7, 2); FAILED FAILED
max2(6, 0, 1, 6, 1); PASSED PASSED
max2(6, 0, 1, 6, 5); FAILED FAILED
max2(7, 5, 4, 7, 5); PASSED PASSED
max2(7, 5, 4, 1, 2); FAILED FAILED
max2(0, 0, 1, 1, 0); PASSED PASSED
max2(0, 0, 1, 4, 6); FAILED FAILED
max2(0, 7, 1, 7, 1); PASSED PASSED
max2(0, 7, 1, 7, 7); FAILED FAILED
max2(7, 7, 3, 7, 7); PASSED PASSED
max2(7, 7, 3, 3, 5); FAILED FAILED
max2(9, 9, 8, 9, 9); PASSED PASSED
max2(9, 9, 8, 1, 8); FAILED FAILED
max2(2, 6, 6, 6, 6); PASSED PASSED
max2(2, 6, 6, 0, 3); FAILED FAILED
max2(8, 0, 1, 8, 1); PASSED PASSED
max2(8, 0, 1, 2, 5); FAILED FAILED
max2(0, 9, 4, 9, 4); PASSED PASSED
max2(0, 9, 4, 7, 8); FAILED FAILED
max2(3, 5, 1, 5, 3); PASSED PASSED
max2(3, 5, 1, 2, 0); FAILED FAILED
max2(1, 6, 4, 6, 4); PASSED PASSED
max2(1, 6, 4, 0, 6); FAILED FAILED
max2(1, 8, 9, 9, 8); PASSED PASSED
max2(1, 8, 9, 8, 4); FAILED FAILED
max2(1, 4, 3, 4, 3); PASSED PASSED
max2(1, 4, 3, 9, 8); FAILED FAILED
max2(8, 0, 8, 8, 8); PASSED PASSED
max2(8, 0, 8, 7, 7); FAILED FAILED
max2(8, 3, 8, 8, 8); PASSED PASSED
max2(8, 3, 8, 3, 7); FAILED FAILED
max2(1, 0, 7, 7, 1); PASSED PASSED
max2(1, 0, 7, 3, 4); FAILED FAILED
Press any key to continue
*/ |