Find the max of three integers

Problem

  • The max function that is declared in the <algorithm> header returns the larger of its two arguments. Write a program that reads three floating-point numbers, uses the max function and displays the larger of all three inputs

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;
}
cs-142/find-the-max-of-three-integers.txt · Last modified: 2015/05/19 11:37 by cs142ta
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0