C++ program (unexpected result)

Joole 1 Reputation point
2021-10-25T22:22:22.653+00:00

Here's the program I'm working on. The average value is not coming out correctly and I cannot figure out why. Any help?

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& maximum, double& minimum)
{
double max = 0, min = 0;

if ((val1 > val2) && (val1 > val3) && (val1 > val4) && (val1 > val5) && (val1 > val6) && (val1 > val7)) {
    max = val1;
}
else if ((val2 > val1) && (val2 > val3) && (val2 > val4) && (val2 > val5) && (val2 > val6) && (val2 > val7)) {
    max = val2;
}
else if ((val3 > val1) && (val3 > val2) && (val3 > val4) && (val3 > val5) && (val3 > val6) && (val3 > val7)) {
    max = val3;
}
else if ((val4 > val1) && (val4 > val2) && (val4 > val3) && (val4 > val5) && (val4 > val6) && (val4 > val7)) {
    max = val4;
}
else if ((val5 > val1) && (val5 > val2) && (val5 > val3) && (val5 > val4) && (val5 > val6) && (val5 > val7)) {
    max = val5;
}
else if ((val6 > val1) && (val6 > val2) && (val6 > val3) && (val6 > val4) && (val6 > val5) && (val6 > val7)) {
    max = val6;
}
else if ((val7 > val1) && (val7 > val3) && (val7 > val4) && (val7 > val5) && (val7 > val6) && (val7 > val2)) {
    max = val7;
}

if ((val1 < val2) && (val1 < val3) && (val1 < val4) && (val1 < val5) && (val1 < val6) && (val1 < val7)) {
    min = val1;
}
else if ((val2 < val1) && (val2 < val3) && (val2 < val4) && (val2 < val5) && (val2 < val6) && (val2 < val7)) {
    min = val2;
}
else if ((val3 < val1) && (val3 < val2) && (val3 < val4) && (val3 < val5) && (val3 < val6) && (val3 < val7)) {
    min = val3;
}
else if ((val4 < val1) && (val4 < val2) && (val4 < val3) && (val4 < val5) && (val4 < val6) && (val4 < val7)) {
    min = val4;
}
else if ((val5 < val1) && (val5 < val2) && (val5 < val3) && (val5 < val4) && (val5 < val6) && (val5 < val7)) {
    min = val5;
}
else if ((val6 < val1) && (val6 < val2) && (val6 < val3) && (val6 < val4) && (val6 < val5) && (val6 < val7)) {
    min = val6;
}
else if ((val7 < val1) && (val7 < val3) && (val7 < val4) && (val7 < val5) && (val7 < val6) && (val7 < val2)) {
    min = val7;
}

}

//function 3

double average(double val1, double val2, double val3, double val4, double val5, double val6, double val7)
{
double ave, sum, min = 0, max = 0;

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 strength1, strength2, strength3, strength4, strength5, strength6, strength7;

    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) << endl;


return 0;

}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Minxin Yu 10,031 Reputation points Microsoft Vendor
    2021-10-26T06:30:18.353+00:00

    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.

    2 people found this answer helpful.

  2. WayneAKing 4,921 Reputation points
    2021-10-25T23:37:52.307+00:00

    Where in high_low(...) are you setting the contents of arguments
    maximum and minimum?

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

  3. WayneAKing 4,921 Reputation points
    2021-10-25T23:46:21.317+00:00

    Function main() is using a variable i - where is it defined?

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

  4. WayneAKing 4,921 Reputation points
    2021-10-26T00:05:34.53+00:00

    In case it's not obvious what needs to be done there, try
    adding these lines at the end of the high_low function:

        minimum = min;
        maximum = max;
    
    • Wayne
    1 person found this answer helpful.
    0 comments No comments