question

Joole-6382 avatar image
0 Votes"
Joole-6382 asked MinxinYu-MSFT edited

C++ program (unexpected result)

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++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What will high_low do if a maximum or minimum value happens to appear twice in the list of values?

0 Votes 0 ·
WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered

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

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered

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

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
1 Vote"
WayneAKing-0228 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MinxinYu-MSFT avatar image
2 Votes"
MinxinYu-MSFT answered MinxinYu-MSFT edited

Hi, @Joole-6382

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.





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @Joole-6382
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·