In: Computer Science
using C++
8. Star Search
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 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. If the user does not enter
valid value, keep displaying error message shown in test cases
until the user types in the valid value. This function should be
called by main once for each
of the 5 judges.
• double calcScore() should 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.
• double findLowest() should find and return the lowest of the 5
scores passed to it.
• double findHighest() should find and return the highest of the 5
scores passed to it.
test case
Judge #1 - Please enter a score between 0.0 and 10.0 : 5 Judge #2 - Please enter a score between 0.0 and 10.0 : 6.5 Judge #3 - Please enter a score between 0.0 and 10.0 : 9.5 Judge #4 - Please enter a score between 0.0 and 10.0 : 5.5 Judge #5 - Please enter a score between 0.0 and 10.0 : 6 Final Score : 6
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution,
please rate the answer. Thanks
===========================================================================
#include<iostream>
using namespace std;
//void getJudgeData() should ask the user for a judge’s
score,
// store it in a reference parameter variable, and validate
it.
void getJudgeData(double &judge){
do{
cout<<"Please enter a score between 0.0 and
10.0: ";
cin>>judge;
if(judge<0.0 || judge>10.0)cout<<"Error:
Enter score between 0.0 and 10.0 only.\n";
}while(judge<0.0 || judge>10.0);
}
//double findLowest() should find and return the lowest of the 5
scores passed to it.
double findLowest(double j1, double j2 ,double j3, double j4,
double j5){
if(j1<=j2 && j1<=j3 && j1<=j4
&& j1<=j5)return j1;
if(j2<=j1 && j2<=j3 && j2<=j4
&& j2<=j5)return j2;
if(j3<=j1 && j3<=j2 && j3<=j4
&& j3<=j5)return j3;
if(j4<=j1 && j4<=j3 && j4<=j2
&& j4<=j5)return j4;
if(j5<=j1 && j5<=j3 && j5<=j4
&& j5<=j2)return j5;
}
//double findHighest() should find and return the highest of the 5
scores passed to it.
double findHighest(double j1, double j2 ,double j3, double j4,
double j5){
if(j1>=j2 && j1>=j3 && j1>=j4
&& j1>=j5)return j1;
if(j2>=j1 && j2>=j3 && j2>=j4
&& j2>=j5)return j2;
if(j3>=j1 && j3>=j2 && j3>=j4
&& j3>=j5)return j3;
if(j4>=j1 && j4>=j3 && j4>=j2
&& j4>=j5)return j4;
if(j5>=j1 && j5>=j3 && j5>=j4
&& j5>=j2)return j5;
}
double calcScore(double j1, double j2 ,double j3, double j4,
double j5){
double total=j1+j2+j3+j4+j5;
double lowest = findLowest(j1,j2,j3,j4,j5);
double highest = findHighest(j1,j2,j3,j4,j5);
return (total-lowest-highest)/3.0;
}
int main(){
double judge_1, judge_2, judge_3, judge_4,
judge_5;
cout<<"Judge #1 - ";getJudgeData(judge_1);
cout<<"Judge #2 - ";getJudgeData(judge_2);
cout<<"Judge #3 - ";getJudgeData(judge_3);
cout<<"Judge #4 - ";getJudgeData(judge_4);
cout<<"Judge #5 - ";getJudgeData(judge_5);
//calculate and display acontestant’s score
double final_score = calcScore(judge_1, judge_2,
judge_3, judge_4, judge_5);
cout<<endl
<<"Final Score -
"<<final_score<<endl;
}
===========================================================