Write a program C++ Write a program that calculates the average of a group of te
ID: 3572304 • Letter: W
Question
Write a program C++
Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions: • void getScore() should ask the user for a test score, store it in a reference param- eter variable, and validate it. This function should be called by main() once for each of the five scores to be entered. • void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main() and should be passed the five scores. • int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage , which uses the function to determine which of the five scores to drop. Input Validation: Do not accept test scores lower than 0 or higher than 100.
Explanation / Answer
Here is the C++ Code for the above given testscores scenario:
#include <iostream>
using namespace Testscores;
// These are the Function Prototypes
void getScore(int &);
void calcAverage(int, int, int, int, int);
int findLowest(int, int, int, int, int);
int main()
{
int Score1, Score2, Score3, Score4, Score5;
// It displays the program introduction that it calculates the average of five scores
cout << " This program calculates the average of five Scores. ";
// we can call getScore function once for each of the five scores
getScore(Score1);
getScore(Score2);
getScore(Score3);
getScore(Score4);
getScore(Score5);
//And also we can call calcAverage and pass the five scores
calcAverage(Score1, Score2, Score3, Score4, Score5);
return 0;
}
void getScore(int &Score)
{
do
{
cout << "Enter a test score: ";
cin >> Score;
if (Score < 0 || Score > 100)
cout << " Invaild test score! "
<< "Test score must be greater than 0 and less than 100. ";
}
} while(Score < 0 || Score > 100);
}
void calcAverage(int Score1, int Score2, int Score3, int Score4, int Score5)
{
int Lowest; //it gives the lowest test score
double Avg; // it gives the average of the four highest test scores
//Here we are calling the function findLowest
Lowest = findLowest(Score1, Score2, Score3, Score4, Score5);
if (Lowest == Score1)
//it caculates average of four highest test scores
Avg = (Score2 + Score3 + Score4 + Score5)/4;
else if(Lowest == Score2)
Avg = (Score1 + Score3 + Score4 + Score5)/4;
else if(Lowest == Score3)
Avg = (Score2 + Score1 + Score4 + Score5)/4;
else if(Lowest == Score4)
Avg = (Score2 + Score3 + Score1 + Score5)/4;
else
Avg = (Score2 + Score3 + Score4 + Score1)/4;
//it display average
cout << " The average of the four highest scores is "
<< Avg << ". ";
}
int findLowest(int Score1, int Score2, int Score3, int Score4, int Score5)
{
if (Score1 < Score2 && Score1 < Score3 && Score1 < Score4 &&
Score1 < Score5)
return Score1;
else if (Score2 < Score1 && Score2 < Score3 && Score2 < Score4 &&
Score2 < Score5)
return Score2;
else if (Score3 < Score2 && Score3 < Score1 && Score3 < Score4 &&
Score3 < Score5)
return Score3;
else if (Score4 < Score2 && Score4 < Score3 && Score4 < Score1 &&
Score4 < Score5)
return Score4;
else
return Score5;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.