Question: Description : Write a C++ program to determine the winner of the contest. The contest has five judges, each of whom awards a score between
Description:
Write a C++ program to determine the winner of the contest. The contest has five judges, each of whom awards a score between 1 and 10 for each of the performers. Fractional scores are not allowed. A contestants final score is determined by dropping the highest and lowest scores received, then averaging the three remaining scores (the average should be rounded to two decimal places). The winner is the contestant with the highest average score. If there is a tie, the winner will be the first contestant judged.
Requirements:
Input the contestants first name followed by the 5 judges scores. You do not know how many contestants there are. Design the loop so the loop terminates when you are finished entering contestants.
Input validation: Do not accept a judges score that is less than 1 or greater than 10. As each score is entered send the score to a function to test the scores validity.
Use function calcAvgScore that has the contestants 5 scores as input parameters
returns the average score for that contestant.
Calls two functions, findLowest and findHighest which both accept the 5 scores as input parameters and return the lowest and highest scores, respectively.
At the end of the program, display the winner and winning score (rounded to 2 decimal places).
This is what I have so far but for some reason there are errors. Please Help!
#include
#include
using namespace std;
int findLowest(int scores[5]) {
int min = scores[0];
for (int i = 1; i < 5; i++) {
if (min > scores[i]) min = scores[i];
}
return min;
}
int findHighest(int scores[5]) {
int max = scores[0];
for (int i = 1; i < 5; i++) {
if (max < scores[i]) max = scores[i];
}
return max;
}
double calcAvgScore(int scores[5]) {
double avg = 0;
for (int i = 0; i < 5; i++) {
avg += scores[i];
}
avg -= findLowest(scores);
avg -= findHighest(scores);
avg /= 3.0;
return avg;
}
int main() {
string names[100];
int scores[100][5], num = 0;
do {
cout << "Enter the contestant's name followed by 5 test scores: ";
cin >> names[num];
bool end = true, valid = true;
for (int i = 0; i < 5; i++) {
cin >> scores[num][i];
if (scores[num][i] != 0) end = false;
if (scores[num][i] > 10 || scores[num][i] < 1) valid = false;
}
if (end == true && names[num] == "Done") break;
else if (valid == false) {
cout << "Invalid score(s)! You will have to enter the name and scores again for the candidate.." << endl;
num--;
}
else num++;
} while (true);
int winnerIndex = 0;
for (int i = 1; i < num; i++) {
if (calcAvgScore(scores[i]) > calcAvgScore(scores[winnerIndex])) winnerIndex = i;
}
cout << fixed << setprecision(2);
cout << endl << "Winner: " << names[winnerIndex] << ", Score: " << calcAvgScore(scores[winnerIndex]) << endl;
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
