Hi, @Joole
Welcome to Microsoft Q&A!
You need to modify the parameters of function 2 and function 3: the average value can be calculated after adding the reference parameters of max and min.
void high_low(double val1, double val2, double val3, double val4, double val5, double val6, double val7, double& max, double& min)
double average(double val1, double val2, double val3, double val4, double val5, double val6, double val7, double& max, double& min)
It is necessary to consider the situation of equality when calculating the algorithm of max and min. Also I made some changes to the high_low() by using loop to make it easier to read.
#include<iostream>
#include <iomanip>
using namespace std;
//function 1
double validation(double validstrength)
{
double value = validstrength;
while ((value < 15) || (value > 75))
{
cout << "Error: the strength should be in the range 15-75.\n"
<< "Please reenter the value of the strength again:";
cin >> value;
}
return value;
}
//function 2
void high_low(double val1, double val2, double val3, double val4, double val5, double val6, double val7, double& max, double& min)
{
double temp[7] = { val1,val2,val3,val4,val5,val6,val7 };
for (int i = 0; i < 7; i++)
{
if (temp[i] > max)
max = temp[i];
if (temp[i] < min)
min = temp[i];
}
}
//function 3
double average(double val1, double val2, double val3, double val4, double val5, double val6, double val7, double& max, double& min)
{
double ave, sum ;
high_low(val1, val2, val3, val4, val5, val6, val7, max, min);
sum = (val1 + val2 + val3 + val4 + val5 + val6 + val7);
ave = (sum - min - max) / 5;
return ave;
}
int main()
{
double min = 0, max = 0;
double strength1, strength2, strength3, strength4, strength5, strength6, strength7;
string i="formul";
cout << "\nEnter the information for formulation " << i << ":" << endl;
//function 1
cout << "\nEnter the compressive strength for formulation " << i << " in MPa from planet 1:";
cin >> strength1;
validation(strength1);
cout << "Enter the compressive strength for formulation " << i << " in MPa from planet 2:";
cin >> strength2;
validation(strength2);
cout << "Enter the compressive strength for formulation " << i << " in MPa from planet 3:";
cin >> strength3;
validation(strength3);
cout << "Enter the compressive strength for formulation " << i << " in MPa from planet 4:";
cin >> strength4;
validation(strength4);
cout << "Enter the compressive strength for formulation " << i << " in MPa from planet 5:";
cin >> strength5;
validation(strength5);
cout << "Enter the compressive strength for formulation " << i << " in MPa from planet 6:";
cin >> strength6;
validation(strength6);
cout << "Enter the compressive strength for formulation " << i << " in MPa from planet 7:";
cin >> strength7;
validation(strength7);
//function 2 & 3
cout << "The average strength for formulation " << i << " was:";
cout << setprecision(2) << average(strength1, strength2, strength3, strength4, strength5, strength6, strength7,max, min) << endl;
return 0;
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.