Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ In general, your program should ask the user for five different test scores,

ID: 3764126 • Letter: C

Question

C++

In general, your program should ask the user for five different test scores, determine which one is the lowest, and calculate and display the average of the remaining four highest scores. The test scores are assumed to be ints. Note that although the test scores in this problem are ints, the average should be calculated as a double. Have it printed out to two decimal places.

Notice that this problem includes a requirement to create three different functions besides the main() function. Here are some programming notes for those functions:

1) void getScore()

The getScore() function should include a prompt to enter another test score, an input statement, and an input validation loop that makes sure the number entered is between 0 and 100 (inclusive). Once the number passes the input validation loop, the function will return the value back through a reference parameter (not the return mechanism of the function). The return data type on the function is therefore void.

2) void calcAverage()

This function will have five input parameters that represent the five test scores for which we are calculating the average. The function will determine the lowest of the five scores by calling the function findLowest(). Once that is determined, it will calculate the average of the remaining four scores and display that information to the screen in a nicely formatted report. (Print out an explanatory statement followed by the average value to two decimal places.) Since it doesn’t pass any data back to the calling program, the return data type on this function is void.

3) int findLowest()

Like calcAverage(), this function will have five input parameters. It will determine which of those five is the lowest and pass that information back through the return mechanism of the function. The return data type on this function should therefore be int.

Note that of the three functions described above, one function (getScore()) will pass data back to the calling function through a reference input parameter, another function (findLowest()) will pass data back to the calling function through the return mechanism of the function, and the third function (calcAverage()) only displays its results and does not pass anything back to the calling function.

Explanation / Answer

#include <iostream>
using namespace std;

void getScore(int &, int &, int &, int &, int &);
void calcAverage(double, double, double, double, double);
int findLowest(int, int,int,int,int);

int main()
{
   int s1, s2, s3, s4, s5;
   getScore(s1, s2, s3, s4, s5);
   cout << "The 5 test score are: " << endl;
   cout << s1 << endl << s2 << endl << s3 << endl << s4 << endl << s5 << endl;

   cout << "Now back in main." << endl;
   cout << "The average of the 4 highest test scores is: ";
   calcAverage(s1, s2, s3, s4, s5);
   cout<< " The Lowest Element is: "<<findLowest(s1,s2,s3,s4,s5)<<endl;
   return 0;
}

void getScore(int &s1, int &s2, int &s3, int &s4, int &s5)
{
       cout << "Enter 5 test scores: ";
       cin >> s1 >> s2 >> s3 >> s4 >> s5;

       while (s1 < 0 || s1 > 100 && s2 < 0 || s2 > 100 && s3 < 0 || s3 > 100 && s4 < 0 || s4 > 100 && s5 < 0 || s5 > 100)
       {
           cout << "Invaild value entered !!" << endl;
           cout << "Please enter a value between 0 through 100: ";
           cin >> s1 >> s2 >> s3 >> s4 >> s5;
       }

}

int findLowest(int a1, int a2, int a3, int a4, int a5)
{
   int array[5];
   array[0]= a1; array[1] = a2; array[2] = a3; array[3] =a4; array[4] = a5;

   //Consider first element as smallest
   int smallest = array[0], i;

   for (i = 0; i < 5; i++) {
       if (array[i] < smallest) {
           smallest = array[i];
       }
   }
   return smallest;

}
void calcAverage(double s1, double s2, double s3, double s4, double s5)
{

cout <<" Average is: "<< (s1 + s2 + s3 + s4 + s5) / 4 << endl;

}