//project01.cpp
////////////////////////////////////////////////////////////
//includes
////////////////////////////////////////////////////////////
#include<iostream.h>
#include<stdlib.h>
////////////////////////////////////////////////////////////
//prototypes
////////////////////////////////////////////////////////////
void swap(int &v1, int &v2);
void max2(int w, int x, int y, int z, int &a, int &b);
bool areMax2(int w, 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
////////////////////////////////////////////////////////////
void swap(int &v1, int &v2)
{
if (v1 != v2)
{
int temp = v1;
v1 = v2;
v2 = temp;
}
}
////////////////////////////////////////////////////////////
//max2
//finds two highest values
////////////////////////////////////////////////////////////
void max2(int w, int x, int y, int z, int &a, int &b)
{
//Sort w, x, y, z in dec order
//put at w's position the highest
if (w < x) swap(w, x);
if (w < y) swap(w, y);
if (w < z) swap(w, z);
//put at x's position the next highest
if (x < y) swap(x, y);
if (x < z) swap(x, z);
//put at y's position the next highest
if (y < z) swap(y, z);
//copy w to a and x to b
a = w;
b = x;
}
////////////////////////////////////////////////////////////
//testMax2
//tests max2 function
////////////////////////////////////////////////////////////
void testMax2(void)
{
int v1, v2, v3, v4, a, b;
for (int i=1; i<=40; i++)
{
v1 = rand()%10;
v2 = rand()%10;
v3 = rand()%10;
v4 = rand()%10;
max2(v1, v2, v3, v4, a, b);
cout << "max2(" << v1 << ", " << v2 << ", "
<< v3 << ", " << v4 << ", " << a << ", "
<< b << "); ";
if(areMax2(v1, v2, v3, v4, a, b))
cout << "PASSED\n";
else
cout << "FAILED\n";
a = rand()%10;
b = rand()%10;
cout << "max2(" << v1 << ", " << v2 << ", "
<< v3 << ", " << v4 << ", " << a << ", "
<< b << "); ";
if(areMax2(v1, v2, v3, v4, a, b))
cout << "PASSED\n";
else
cout << "FAILED\n";
}
}
////////////////////////////////////////////////////////////
//areMax2
//checks if the values are two highest of the given four
////////////////////////////////////////////////////////////
//areMax2
/*
sort w, x, y, z in increasing order
put z in ta, and y in tb
do the following 3 times
if w>x then swap w, x
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 areMax2(int w, int x, int y, int z, int a, int b)
{
int ta, tb;
for (int i=1; i<=3; i++)
{
if (w>x) swap (w, x);
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, 0, 7, 4); PASSED
max2(1, 7, 4, 0, 9, 4); FAILED
max2(8, 8, 2, 4, 8, 8); PASSED
max2(8, 8, 2, 4, 5, 5); FAILED
max2(1, 7, 1, 1, 7, 1); PASSED
max2(1, 7, 1, 1, 5, 2); FAILED
max2(7, 6, 1, 4, 7, 6); PASSED
max2(7, 6, 1, 4, 2, 3); FAILED
max2(2, 2, 1, 6, 6, 2); PASSED
max2(2, 2, 1, 6, 8, 5); FAILED
max2(7, 6, 1, 8, 8, 7); PASSED
max2(7, 6, 1, 8, 9, 2); FAILED
max2(7, 9, 5, 4, 9, 7); PASSED
max2(7, 9, 5, 4, 3, 1); FAILED
max2(2, 3, 3, 4, 4, 3); PASSED
max2(2, 3, 3, 4, 1, 1); FAILED
max2(3, 8, 7, 4, 8, 7); PASSED
max2(3, 8, 7, 4, 2, 7); FAILED
max2(7, 9, 3, 1, 9, 7); PASSED
max2(7, 9, 3, 1, 9, 8); FAILED
max2(6, 5, 0, 2, 6, 5); PASSED
max2(6, 5, 0, 2, 8, 6); FAILED
max2(0, 2, 4, 8, 8, 4); PASSED
max2(0, 2, 4, 8, 6, 5); FAILED
max2(0, 9, 0, 0, 9, 0); PASSED
max2(0, 9, 0, 0, 6, 1); FAILED
max2(3, 8, 9, 3, 9, 8); PASSED
max2(3, 8, 9, 3, 4, 4); FAILED
max2(6, 0, 6, 6, 6, 6); PASSED
max2(6, 0, 6, 6, 1, 8); FAILED
max2(4, 9, 6, 3, 9, 6); PASSED
max2(4, 9, 6, 3, 7, 8); FAILED
max2(8, 2, 9, 1, 9, 8); PASSED
max2(8, 2, 9, 1, 3, 5); FAILED
max2(9, 8, 4, 0, 9, 8); PASSED
max2(9, 8, 4, 0, 7, 6); FAILED
max2(3, 6, 1, 5, 6, 5); PASSED
max2(3, 6, 1, 5, 4, 2); FAILED
max2(0, 9, 7, 3, 9, 7); PASSED
max2(0, 9, 7, 3, 7, 2); FAILED
max2(6, 0, 1, 6, 6, 6); PASSED
max2(6, 0, 1, 6, 5, 7); FAILED
max2(5, 4, 1, 2, 5, 4); PASSED
max2(5, 4, 1, 2, 0, 0); FAILED
max2(1, 4, 6, 0, 6, 4); PASSED
max2(1, 4, 6, 0, 7, 1); FAILED
max2(7, 7, 7, 7, 7, 7); PASSED
max2(7, 7, 7, 7, 3, 3); FAILED
max2(5, 9, 9, 8, 9, 9); PASSED
max2(5, 9, 9, 8, 1, 8); FAILED
max2(2, 6, 6, 0, 6, 6); PASSED
max2(2, 6, 6, 0, 3, 8); FAILED
max2(0, 1, 2, 5, 5, 2); PASSED
max2(0, 1, 2, 5, 0, 9); FAILED
max2(4, 7, 8, 3, 8, 7); PASSED
max2(4, 7, 8, 3, 5, 1); FAILED
max2(2, 0, 1, 6, 6, 2); PASSED
max2(2, 0, 1, 6, 4, 0); FAILED
max2(6, 1, 8, 9, 9, 8); PASSED
max2(6, 1, 8, 9, 8, 4); FAILED
max2(1, 4, 3, 9, 9, 4); PASSED
max2(1, 4, 3, 9, 8, 8); FAILED
max2(0, 8, 7, 7, 8, 7); PASSED
max2(0, 8, 7, 7, 8, 3); FAILED
max2(8, 3, 7, 1, 8, 7); PASSED
max2(8, 3, 7, 1, 0, 7); FAILED
max2(3, 4, 9, 6, 9, 6); PASSED
max2(3, 4, 9, 6, 5, 1); FAILED
max2(0, 9, 9, 6, 9, 9); PASSED
max2(0, 9, 9, 6, 8, 3); FAILED
max2(4, 8, 4, 9, 9, 8); PASSED
max2(4, 8, 4, 9, 9, 2); FAILED
max2(5, 5, 3, 3, 5, 5); PASSED
max2(5, 5, 3, 3, 3, 7); FAILED
max2(4, 3, 8, 0, 8, 4); PASSED
max2(4, 3, 8, 0, 8, 8); FAILED
max2(0, 6, 8, 1, 8, 6); PASSED
max2(0, 6, 8, 1, 9, 8); FAILED
max2(9, 7, 2, 2, 9, 7); PASSED
max2(9, 7, 2, 2, 8, 2); FAILED
...
*/
|