write a function that computes the maximum and the minimum numbers from a given
ID: 3912337 • Letter: W
Question
write a function that computes the maximum and the minimum numbers from a given list of four numbers. the maximum number is defined as the largest number in the list while the minimum number is defined as the smallest. the function should have four parameters of n1, n2, n3, and n4 for the four numbers entered and will call two other functions. embed the function in the driver program that wants you to test the function again and again until you till the program that you are finished. the answer should be in C++
Explanation / Answer
#include<iostream>
using namespace std;
//declare a function to find the maximum and minimum of 4 numbers n1,n2,n3 and n4 and return the maximum and minimum through reference variable passed from main
void max_minimum(int n1, int n2, int n3, int n4, int &max, int &min);
//declare a function max which find the maximum among two numbers
int min(int n1, int n2);
//declare function min which find the minimum among two numbers
int max(int n1, int n2);
int main()
{
int max1, min1;
//call the function with four numbers
max_minimum(8, 9, 19, 2,max1,min1);
cout << "Maximum number is : " << max1 << " , minimum number is : " << min1 << endl;
}
//function definitions
void max_minimum(int n1, int n2, int n3, int n4, int &max1, int &min1)
{
int ret1, ret2;
//find maximum of two numbers n1 and n2 and maximum of n3 and n4
ret1 = max(n1, n2);
ret2 = max(n3, n4);
//now find the max among ret1 and ret2 and assign it to the reference variable passed from main max1
max1 = max(ret1, ret2);
//repeat above steps for finding minimum and assign it to the reference variable passed from main min1
ret1 = min(n1, n2);
ret2 = min(n3, n4);
min1 = min(ret1, ret2);
}
int min(int n1, int n2)
{
if (n2 < n1)
return n2;
else
return n1;
}
int max(int n1, int n2)
{
if (n2 > n1)
return n2;
else
return n1;
}
/*output
Maximum number is : 19 , minimum number is : 2
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.