Hello here is my problem. My program needs to calculate the average score for an athlete. It needs to allow the user to enter the athlete's number, then ask for the number of judges, their scores, and calculate the min, max, and average. The average however needs to be of all the scores except the min and max. The problem occurs because the program gives me the first and last inputted values as the min/max and not the actual minimum or maximum value. I have attached the code and a sample output for the program. Please Help =D
#include <iostream>
usingnamespace std;
int main()
{
int id, numjudge;
double max,min,avg,scorecount=0;
double score[10];
cout<<"Hello this program will calculate average score for an athelete"<<endl<<endl;
cout<<"In order to stop the program enter a negative value for ID"<<endl<<endl;
cout<<"enter ID of an athlete"<<endl;
cin>>id;
while (id>=0)
{
cout<<"now enter the the number of judges judging the contest"<<endl;
cin>>numjudge;
cout<<"now enter the score of each judge"<<endl;
for (int i=0;i<numjudge;i++)
{
cin>>score[i];
min=score[0];
max=score[0];
if(score[i]>=max){
max=score[i];}
if (score[i]<=min){
min=score[i];
}
scorecount=scorecount+score[i];
}
cout<<"min is "<<min<<" max is "<<max;
avg=(scorecount-min-max)/(numjudge);
cout<<"average for the athlete is "<<avg<<endl;
cout<<"enter the id of the next athlete"<<endl;
cin>>id;
}
return 0;
}
Hello this program will calculate average score for an athelete
In order to stop the program enter a negative value for ID
enter ID of an athlete
9090
now enter the the number of judges judging the contest
7
now enter the score of each judge
9
0
6
12
2
6
11
min is 9 max is 11average for the athlete is 3.71429
enter the id of the next athlete
88890
now enter the the number of judges judging the contest
9
now enter the score of each judge
6
7
0
1
2
4
13
9
5
min is 5 max is 6average for the athlete is 9.11111
enter the id of the next athlete
for (int i=0;i<numjudge;i++)
{
cin>>score[i]; //consider you enter the number 11 (the last one)
min=score[0]; //then here you set min on 9 (the first score you entered)
max=score[0]; //here you set max on 9
if(score[i]>=max) // if 11 >= 9 => yes
max=score[i]; // set max on 11
if (score[i]<=min) // if 11 <= 9 => no
min=score[i]; // don't do this and keep 9 as minimum
}
Since you always set min and max to score[0] (in your example to 9 again)
Whatever happens in between inserting 9 and 11 doesn't matter
You actually determine the min and max value of your first and last score only
Btw:
1 2
avg=(scorecount-min-max)/(numjudge); // why subtract min and max ?
avg = scorecount / numjudge; //this is the actual average
I'm not a fan of providing a solution now. Maybe someone will but I think it's better not to.