Help to figure out problem

Hello i am trying to create a program that will count the average for each athlete in the Olympic games. The trick is that i have to remove them maximum score given by a judge and the minimum score. I have this so far: the program works but the average does not come up to right numbers. Help please.
#include <iostream>
using namespace 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];
for (int k=0;k<10;k++){
min=score[0];
max=score[0];
if(score[i]>max){
max=score[i];
}
else if (score[i]<min)
{
min=score[i];
}
}
scorecount=scorecount+score[i];
}
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 mkory,

Next time use the code tags ([_code] and [_\code]--remove the "_") so that this is easier to read. May you elaborate on what issues you are getting? Maybe some sample input and sample output?

I believe that your mistake is that you are updating your min and max constantly within your inner loop. This needs to happen only once per judging round.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;
int main( void ){
	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;
		min=999999999; /* Enter some arbitrarily large number */
		max=0; /* Enter some arbitarily low number */
		for (int i=0;i<numjudge;i++){
			cin>>score[i];
			for (int k=0;k<10;k++){
				/* You updated your max and min to score[0].*/
				if(score[i]>max){
					max=score[i];
				}else if (score[i]<min){
					min=score[i];
				}
			}
				scorecount=scorecount+score[i]; /* scorecount += score[i]; */
		}
		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;
}


Recommendations:
1. Add an exception prevention on that numjudge command, because if the user enters 11 you will go out of the bounds of your array.

2. It seems a bit strange for you to re-define how many judges you want every time you input a new id. I would recommend doing this only at the beginning of the program once. However, I don't know your requirements, so take that with a grain of salt.

3. What happens if your max or min are repeated? e.g. 5 judges enter a score of 10. Shoud all of them be removed? Or just one?
4. You need to handle the case where there is only one judge. If there is only one judge, you will end up with a negative score. I don't know if you want this behavior or if you would prefer 0 being the minimum score.


In short: Consider all of your test cases. These may be contributing to the incorrect answer issue.
Last edited on
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
2365
now enter the the number of judges judging the contest
7
now enter the score of each judge
8.8
7.4
6.3
7.1
5.6
7.3
6.4
average for the athlete is 4.81429
enter the id of the next athlete
4580
now enter the the number of judges judging the contest
7
now enter the score of each judge
8.9
9.4
2.3
5.6
3.4
9.9
9.9
average for the athlete is 11.3571
enter the id of the next athlete
here is a simple output average is not the value i am looking for
Did you try any of the recommendations I made? I think the main issue is where you are updating your min and max.
Topic archived. No new replies allowed.