Table of Contents

Find the max of three integers

Problem

Solution

/*
Test case 1: 
Inputs: 5,6,7 (three different numbers)
Expected Output: 7
Actual Output: 7
 
Test case 2:
Inputs: 5,6,6 (two numbers the same)
Expected Output: 6
Actual Output: 6
 
Test case 3:
Inputs: 5,5,5 (all three numbers the same)
Expected Output: 5
Actual Output: 5
*/
#include <iostream>
#include <algorithm>
 
using namespace std;
 
/*
	Find the max of three integers
	@param int1 first integer
	@param int2 second integer
	@param int3 third integer
	@return the max of int1, int2, int3
*/
int find_max_of_three(int int1, int int2, int int3)
{
	int return_var;
 
	// code to modify return_var to be max of three ints
 
	// find the max of the first two, keep track of that
	int max_of_first_two = max(int1, int2);
 
	// find max of the last and the result of previous step
	return_var = max(max_of_first_two, int3);
 
	//this is an alternate way to do this:
	// return_var = max(max(int1, int2), int3);
	// the inner-most function is evaluated first
 
	return return_var;
}
 
int main()
{
	cout << "Give me three integers: ";
 
	// declare three storage variables
	int a, b, c;
 
	// read in three integers without checking for bad input
	cin >> a >> b >> c;
 
	// function to find max of three numbers
	int max = find_max_of_three(a, b, c);
 
	// print result
	cout << "The max of " << a << ", " << b << ", and " << c  << " is " << max << endl;
 
	system("pause");
	return 0;
}