I need help writing this C++ program: 1. Write a complete C++ program that takes
ID: 3847240 • Letter: I
Question
I need help writing this C++ program:
1. Write a complete C++ program that takes the judges’ scores for a gymnastic event and finds each contestant’s score. It should satisfy all the following specifications: it first reads in an integer N that gives the number of judges. It then reads in a contestant number followed by N integers which are the scores given the contestant by the N judges. The scores should be read into an array, and then the program calls a function called findAverageScore that takes N and the array of judges’ scores as arguments. The function should find and drop the highest score and the lowest score, and then find and return the average of the remaining N-2 scores. The main should then print the contestant number and the average score s/he receives for the event to the screen. It will continue to do this (read in the contestant number, then the N integers, then print the contestant number and the calculated score on a new line) until a negative value is entered for the contestant number. The program will then print the contestant number who scored the highest. (You may assume no ties will occur.) All input will be from the keyboard and you may also assume that N will never be greater than 10 and never less than 3. You may format the output in any way as long as it is readable and correct, and you may assume all inputs are correct. (JUST WRITE CODE; NO DOCUMENTATION REQUIRED except for your NAME and ID at the start of the file!)
Name your program prob1.cpp. Submit it to the EPP - Problem 1 section on the class Titanium site. If you wrote more than one file containing code, make sure to send and copy them as well. Remember to put in your name and ID at the start of each file.
Sample output screen for a session (Bold numbers are printed by the program; normal numbers are typed in by the user.)
Number of Judges: 5
34 75376 Contestant 34 6
28 858107 Contestant 28 7.666666667 3 55344 Contestant 3 4.333333333
1
Contestant 28 had the highest score. <program ends>
Explanation / Answer
Your program can be broken down into simple steps. Its always a a good thing to design an algorithm by breaking the problem into subproblems. It eases the work and we can easily achieve what we are looking for.
1.Accept N , the number of judges
2.Read contestant number
3.Read score by each judge
4.Call findAverageScore to get the score for each contestant.
5.Depending on the value returned by above function call decide which contestant has highest score.
lets get started now. Heres's the code.
#include <iostream>
using namespace std;
int findAverageScore(int[],int);
int main() {
// your code goes here
int n;
int max=0;
int con;
int cn;
int i;
cout <<"Enter the number of judge"s<<endl;
cin >>n;
int s[n];
while(true){
cout <<"Contestant Number"<<endl;
cin >>cn;
if(cn>0){
cout <<"Contestant Scores"<<endl;
for(i=0;i<n;i++)
cin >>s[i];
int score=findAverageScore(s,n);
cout <<"Contestant "<<cn<<" "<<score<<endl;
if(score>max){max=score;con=cn;}
}
else {cout <<"Contestant"<<con<<"has the highest score"<<endl;break;}
}
return 0;
}
int findAverageScore(int ss[],int nn)
{
int a=0,b=ss[0],sc=0,i;
for(int i=0;i<nn;i++)
{
if(ss[i]>=a)a=ss[i];
if(ss[i]<=b)b=ss[i];
sc=sc+ss[i];
}
sc=sc-a-b;
sc=sc/(nn-2);
return sc;
}
I have used variables max and con to store the highest score and the contestant number of highest scorer. Every time i get the average score of any contestant from the function findAverageScore I compare it with previous max which is initially ZERO so I get the highest scorer's CONTESTANT NUMBER eventually. In the function findAverageScore I have initialised a and b as 0 and ss[0] and then I compare them with each element of scores array to get the minimum and maximum score values. Then all we need to do is add the scores and subtract a and b from the sum.
I hope it helps you.