In: Computer Science
C++ Functions
There are many reasons why one would want to know the minimum and the maximum of a series of values: fight control, range determinate, even in academia, professors often seek the distribution of grades on homework, exams, and class grades.
For this lab, you will write a program which will include a minimum function and a maximum function. Both functions should take in three values. The minimum function should return the minimum of the three values and the maximum should return the maximum of the three values.
The values should be entered by the user.
So, at the very latest, your program should have three functions:
- main()
- minimum()
- maximum()
Beyond that, feel free to modularize your code as you wish.
Screenshot of the code:
Sample Output:
Code to copy:
//Include required header file.
#include <iostream>
//Use the standard naming convention.
using namespace std;
//Define the function maximum() having three integer
//values.
int maximum(int num1, int num2, int num3)
{
//Declare and initialize required variables.
int maxNum = 0;
//If num1 is greater than or equal to num2 and num3,
//then make num1 as maximum number.
if((num1 >= num2) && (num1 >= num3))
{
maxNum = num1;
}
//If num2 is greater than or equal to num1 and num3,
//then make num2 as maximum number.
if((num2 >= num1) && (num2 >= num3))
{
maxNum = num2;
}
//If num3 is greater than or equal to num1 and num2,
//then make num3 as maximum number.
if((num3 >= num1) && (num3 >= num2))
{
maxNum = num3;
}
//Return the maximum of the three values given in
//the function.
return maxNum;
}
//Define the function minimum() having three
//integer values.
int minimum(int num1, int num2, int num3)
{
//Declare and initialize required variables.
int minNum = 0;
//If num1 is less than or equal to num2 and num3,
//then make num1 as minimum number.
if((num1 <= num2) && (num1 <= num3))
{
minNum = num1;
}
//If num2 is less than or equal to num1 and num3,
//then make num2 as minimum number.
if((num2 <= num1) && (num2 <= num3))
{
minNum = num2;
}
//If num3 is less than or equal to num1 and num2,
//then make num3 as minimum number.
if((num3 <= num1) && (num3 <= num2))
{
minNum = num3;
}
//Return the minimum of the three values given in
//the function.
return minNum;
}
//Start the execution of the main() method.
int main()
{
//Declare required variables.
int val1, val2, val3, minVal, maxVal;
//Prompt the user to enter three integer values.
cout << "Enter three values:" << endl;
cin >> val1 >> val2 >> val3;
//Call the function maximum() by passing these three
//values and store the returned value into the
//variable maxVal.
maxVal = maximum(val1, val2, val3);
//Call the function minimum() by passing these three
//values and store the returned value into the
//variable minVal.
minVal = minimum(val1, val2, val3);
//Display the maximum value among these three values.
cout << "The maximum of three values is: " << maxVal;
cout << endl;
//Display the minimum value among these three values.
cout << "The minimum of three values is: " << minVal;
cout << endl;
return 0;
}