Code using C++ and instead of using individual variables, use arrays. A particul
ID: 3860636 • Letter: C
Question
Code using C++ and instead of using individual variables, use arrays. A particular talent competition has 5 judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3, are not allowed. A performer's final score is determined by dropping the highest and lowest score received, then averaging the 3 remaining scores. Write a program that uses these rules to calculate and display a contestant's score. It should include the following functions: void getjudgeData() should ask the user for a judge's score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the 5 judges. double calcScore(int, int, int, int, int) calculate and return the average of the 3 scores that remain after dropping the highest and lowest scores the performer received. This function should be called just once by main and should be passed the 5 scores. Two additional functions, described below, should be called by calcScore, which uses the returned information to determine which of the scores to drop. int findLowest(int, int, int, int, int) should find and return the lowest of the 5 scores passed to it. int findHighest(int, int, int, int, int) should find and return the highest of the 5 scores passed to it.Explanation / Answer
#include<iostream>
using namespace std;
//Function to accept judge's score
void getJudgeData(int &score)
{
//Loop to validate score between 0 and 10
do
{
cout<<" Enter Judge's score (between 0 and 10): ";
//Accept score
cin>>score;
//Checks score whether it is between 0 and 10 or not
if(score < 0 || score > 10)
//Shows error
cout<<" Error: Invalid Score. Score must be between 0 and 10";
else
break;
}while(1);//End of do-while loop
}//End of function
//Function to return lowest score
int findLowest(int judgeScore[])
{
//Initializes the low score to first index position of the judgeScore
int low = judgeScore[0];
//Loops from one position to 4th index position
//Because zero index position is taken into consideration earlier
for(int c = 1; c < 5; c++)
{
//Checks whether the current index position score is less than the earlier low score or not
if(judgeScore[c] < low)
//Update the low score to the current index position of the score
low = judgeScore[c];
}//End of loop
//Returns the lowest score
return low;
}//End of function
//Function to return highest score
int findHighest(int judgeScore[])
{
//Initializes the high score to first index position of the judgeScore
int high = judgeScore[0];
//Loops from one position to 4th index position
//Because zero index position is taken into consideration earlier
for(int c = 1; c < 5; c++)
{
//Checks whether the current index position score is greater than the earlier high score or not
if(judgeScore[c] > high)
//Update the high score to the current index position of the score
high = judgeScore[c];
}//End of loop
//Return the highest score
return high;
}//End of function
//Returns the sum of scores
double calScore(int judgeScore[])
{
//Initializes the final score
double finalScore = 0;
//Find out the lowest score
int low = findLowest(judgeScore);
//Find out the highest score
int high = findHighest(judgeScore);
//Loops five times
for(int c = 0; c < 5; c++)
{
//Checks for not low or high score
//Ignores the lowest and highest score
if(judgeScore[c] != low && judgeScore[c] != high)
{
//Calculates total
finalScore += judgeScore[c];
}//End of if
}//End of loop
//Returns final score
return finalScore;
}//End of function
//Main function definition
int main()
{
//To store score
int score;
//To store five judge's score
int judgeScore[5];
//loops five times
for(int x = 0; x < 5; x++)
{
//Accepts judge's score
getJudgeData(score);
//Stores judge score in array
judgeScore[x] = score;
}//End of loop
cout<<" Five judge's scores: ";
//Loops five times to displays judge's score
for(int x = 0; x < 5; x++)
cout<<" No: "<<x+1<<" Score = "<<judgeScore[x];
//Displays final score
cout<<" Final Score = "<<calScore(judgeScore);
}//End of main
Sample Run 1:
Enter Judge's score (between 0 and 10): 12
Error: Invalid Score. Score must be between 0 and 10
Enter Judge's score (between 0 and 10): 5
Enter Judge's score (between 0 and 10): 7
Enter Judge's score (between 0 and 10): 9
Enter Judge's score (between 0 and 10): 1
Enter Judge's score (between 0 and 10): 3
Five judge's scores:
No: 1 Score = 5
No: 2 Score = 7
No: 3 Score = 9
No: 4 Score = 1
No: 5 Score = 3
Final Score = 15
Sample Run 2:
Enter Judge's score (between 0 and 10): 4
Enter Judge's score (between 0 and 10): 6
Enter Judge's score (between 0 and 10): 3
Enter Judge's score (between 0 and 10): 8
Enter Judge's score (between 0 and 10): 1
Five judge's scores:
No: 1 Score = 4
No: 2 Score = 6
No: 3 Score = 3
No: 4 Score = 8
No: 5 Score = 1
Final Score = 13