need to design a code for my first assignment in dev c++ for olmypic judging, 5 judges, 10 competitors, need to score each competitor between 0.0 and 5.0. i cant use the highest score for each competitor or the lowest and must average the remaining results. any help? i have it started but not working correctly
clubberlangMayo
 2 Contributors
 forum9 Replies
 12 Views
 5 Years Discussion Span
 comment Latest Post by clubberlangMayo
Gonbe 32
i have it started but not working correctly
How about you start with posting that?
edit And don't only post it, also state what you were trying to do and (if possible) where the code differs from your expectations.
Edited
by Gonbe
clubberlangMayo
/*excuse my ignorance, first timw poster, having a bit of freak trying to figure this out. early days programming :)
i must get the input for each of the 5 judges. we say there is 5 competitors, score each competitor from the 5 judges .score range between 0.0 and 5.0.
when the scoring is complete, i must get the highest and lowest score for each competitor and ignore it . then use the remaining score and get the average of it
*/
#include<iostream>
#include<iomanip>
using namespace std;
int numOfCompetitors;
double Competitor1, Competitor2;
double minScore, maxScore, avgScore;
double judgeEntry1, judgeEntry2;
double finalScore, lowestScore, highestScore;
double judgeScore;
double score;
int main()
{
cout<<"now that the first competitor has finished performed, you the judges will award the scores"<<endl;
cout<<"judge 1, please enter your score for competitor number 1 "<<endl;
cin>>judgeEntry1;
cout<<" thank you judge 1"<<endl;
cout<<"judge 2 please enter your score for competitor number 1 "<<endl;
cin>>judgeEntry2;
cout<<"thank you judge 2"<<endl;
Competitor1=judgeEntry1 + judgeEntry2;
cout<<"competitor number 1, you score a total of "<<Competitor1<<endl;
if (judgeScore >=0.0 && judgeScore <=5.0) {
finalScore += judgeScore;
lowestScore = judgeScore;
highestScore = judgeScore;
if (judgesScore < lowestScore)
{ //determine lowest score to be awarded to the competitor
competitor
lowestScore = judgeScore;
judgesScore = minScore;
}
if (judgesScore > highestScore)
{ //determine highest Score to be awarded to the competitor
competitor
highestScore = judgesScore
}
system ("pause");
return 0;
}
Gonbe 32

Posting it all in "Code" because the forum keeps whining about invalid formatting, even though it's not..
Hmm okay, starting from the start. I'd first recommend defining a couple of constants:
// The amount of judges that will be judging. const unsigned int NUMBER_OF_JUDGES = 5; // The amount of competators. const unsigned int NUMBER_OF_COMPETATORS = 10;
Next you'd have to identify what it is what you want your application to do. In this case you seem to want every judge judge every competator. This strongly suggests like repetition which means you'll likely want to use a loop construct. In pseudocode it'll look something like this:
For every competitor:
Ask every judge for a grade of this competator.
Perform calculations based on the grades.This would translate to something like:
// For every competator.. for (unsigned int competator = 1; competator <= NUMBER_OF_COMPETATORS; competator++) { // Go past every judge.. for (unsigned int judge = 1; judge <= NUMBER_OF_JUDGES; judge++) { // .. and ask it for a grade for the current competator } // Do "stuff" with the grades. }
Next, before we start obtaining grades we'll need a place to store them at. There's a variety of tools for this, but for simplicity we'll be going for arrays. We can do this because the amount of grades required is known at compiletime. The code would change as follows:
Next we'll have to ask a judge for a grade. Note that in your code this happens pretty much in the same way for every judge. This is a good sign you should probably define this operation as a function. This would transform the code as follows:
// For every competator.. for (unsigned int competator = 1; competator <= NUMBER_OF_COMPETATORS; competator++) { // Grades for the current competator. double grades[NUMBER_OF_JUDGES]; // Go past every judge.. for (unsigned int judge = 1; judge <= NUMBER_OF_JUDGES; judge++) { // .. and ask it for a grade for the current competator grades[judge  1] = ObtainGrade(competator, judge); } // Do "stuff" with the grades. }
We're calling a function called "ObtainGrade" here. This function expects a competator and a judge, which are represented as integers at the moment. It results in a "double" which is the type used to represent a score here. We store the grade of judge "judge" for competator "competator" at index "judge  1" in the array of grades. (Arrays start at index 0 while our first judge is judge number 1, hence the "1")
Now, what should our "ObtainGrade" function do exactly? It should ask the judge for a grade, but a user could enter wrong input. A typical construct used for these types of scenario's would be a dowhile loop It will ask for input atleast once, and repeats it until a correct value is obtained.
The function could look something like this:
double ObtainGrade (const unsigned int competator, const unsigned int judge) { double score = 0; do { cout<<"Judge " << judge << ", please enter your score for competitor number " << competator << ": "; cin >> score; } while (!IsValidScore(score)); cout<<"Thank you, judge " << judge << ".\n"; return score; }
Note that a new function is added for validating the score. In our case the only condition is that the score entered is between 0.0 and 5.0 (exclusive on both sides):
// Boundaries for the score entered. const double SCORE_UPPER_BOUNDARY = 5.0; const double SCORE_LOWER_BOUNDARY = 0.0; bool IsValidScore(const double score) { return (score > SCORE_LOWER_BOUNDARY && score < SCORE_UPPER_BOUNDARY); }
Again we use global constants to defined these "special" values.
If a user enters wrong input no message is shown though, this is probably confusing. You could modify the function as follows to fix that:
double ObtainGrade (const unsigned int competator, const unsigned int judge) { double score = 0; bool validScore = true; do { // The score entered was invalid. if (!validScore) { cout << "Invalid score!\n"; } cout<<"Judge " << judge << ", please enter your score for competitor number " << competator << ": "; cin >> score; // Determine if the entered score was valid or not. validScore = IsValidScore(score); } while (!validScore); cout<<"Thank you, judge " << judge << ".\n"; return score; }
The application could still behave in an undesired way when something like "abc" is entered as input. You could guard yourself against this by modifying the function as follows:
double ObtainGrade (const unsigned int competator, const unsigned int judge) { double score = 0; bool validScore = true; do { // The score entered was invalid. if (!validScore) { cout << "Invalid score!\n"; } cout<<"Judge " << judge << ", please enter your score for competitor number " << competator << ": "; cin >> score; // Something bad happened on the stream if (!cin) { // Clear error flags and discard characters remaining in the buffer. cin.clear(); // Something invalid was entered, this can never be a valid score. validScore = false; } else { // Determine if the entered score was valid or not. validScore = IsValidScore(score); } // Clear possible additional values entered. cin.ignore(numeric_limits<streamsize>::max(), '\n' ); } while (!validScore); cout<<"Thank you, judge " << judge << ".\n"; return score; }
It's still not bulletproof but I doubt you need anything more advanced. (This is probably too advanced already. If so, just discard this and use the easier solution posted earlier)
We now have a construction to obtain grades for competators, but you still need something to obtain the statistics. The STL contains a bunch of functions to do this, but I think it might be easier to define functions yourself. So, what we need is the minimum, maximum and average. So let's start off with creating variables to store these values:
// Statistics. double lowestGrade = 0; double highestGrade = 0; double averageGrade = 0;
These would be declared inside the competator loop. It might be easiest to define a function that asks for the grade array and extracts a specific statistic. The downside would be that you'd have to loop over all values 3 times. Instead, we'll be obtaining the 3 statistics in one pass, I think this is still comprehendable:
void ObtainStatistics (const double array[], const unsigned int size, double& lowest, double& highest, double& average) { // We can't obtain statistics from an empty array. Should probably "assert" this. if (size > 0) { lowest = array[0]; highest = array[0]; average = array[0]; for (unsigned int i = 1; i < size; i++) { // We found a new "lowest". if (array[i] < lowest) { lowest = array[i]; } // We found a new "highest". else if (array[i] > highest) { highest = array[i]; } // Update the average. average = ((average * i) + array[i]) / (i + 1); } } }
Last, all we need to do is show the statistics. You could do this in a function, but I just did it in the loop.. The code would look like:
#include<iostream> #include<limits> using namespace std; // The amount of judges that will be judging. const unsigned int NUMBER_OF_JUDGES = 5; // The amount of competators. const unsigned int NUMBER_OF_COMPETATORS = 10; // Boundaries for the score entered. const double SCORE_UPPER_BOUNDARY = 5.0; const double SCORE_LOWER_BOUNDARY = 0.0; // Function declarations/prototypes. bool IsValidScore (const double score); double ObtainGrade (const unsigned int competator, const unsigned int judge); void ObtainStatistics (const double array[], const unsigned int size, double& lowest, double& highest, double& average); // Predicate to determine if an entered score is valid or not. bool IsValidScore(const double score) { return (score > SCORE_LOWER_BOUNDARY && score < SCORE_UPPER_BOUNDARY); } double ObtainGrade (const unsigned int competator, const unsigned int judge) { double score = 0; bool validScore = true; do { // The score entered was invalid. if (!validScore) { cout << "Invalid score!\n"; } cout<<"Judge " << judge << ", please enter your score for competitor number " << competator << ": "; cin >> score; // Something bad happened on the stream if (!cin) { // Clear error flags and discard characters remaining in the buffer. cin.clear(); // Something invalid was entered, this can never be a valid score. validScore = false; } else { // Determine if the entered score was valid or not. validScore = IsValidScore(score); } // Clear possible additional values entered. cin.ignore(numeric_limits<streamsize>::max(), '\n' ); } while (!validScore); cout<<"Thank you, judge " << judge << ".\n"; return score; } void ObtainStatistics (const double array[], const unsigned int size, double& lowest, double& highest, double& average) { // We can't obtain statistics from an empty array. Should probably "assert" this. if (size > 0) { lowest = array[0]; highest = array[0]; average = array[0]; for (unsigned int i = 1; i < size; i++) { // We found a new "lowest". if (array[i] < lowest) { lowest = array[i]; } // We found a new "highest". else if (array[i] > highest) { highest = array[i]; } // Update the average. average = ((average * i) + array[i]) / (i + 1); } } } int main() { // For every competator.. for (unsigned int competator = 1; competator <= NUMBER_OF_COMPETATORS; competator++) { // Grades for the current competator. double grades[NUMBER_OF_JUDGES]; // Statistics. double lowestGrade = 0; double highestGrade = 0; double averageGrade = 0; // Go past every judge.. for (unsigned int judge = 1; judge <= NUMBER_OF_JUDGES; judge++) { // .. and ask it for a grade for the current competator grades[judge  1] = ObtainGrade(competator, judge); } // We now have "NUMBER_OF_JUDGES" grades for this competator. // Obtain the statistics ObtainStatistics(grades, NUMBER_OF_JUDGES, lowestGrade, highestGrade, averageGrade); cout << "Statistics for competator " << competator << ":\n" << "The lowest grade received: " << lowestGrade << ".\n" << "The highest grade received: " << highestGrade << ".\n" << "The average grade received: " << averageGrade << ".\n"; } return 0; }
Note that this is just a quick example of what you could be doing, there's a lot of variations possible. One of the things I'd probably change if this wasn't an example was to not implement score by using a single double variable, but use two integers instead. (or if the grading system may be adjusted, for example by scoring 0  50 instead of 0  5.0, a single integer) But this should get you started at least, or atleast raise additional questions.
clubberlangMayo
your help was greartly appreciated..have worked the looping at the moment for the competitors and the average formula for the scoring without highest and lowest numbers.getting there. it takes alot of practice. thank u kindly.
clubberlangMayo
was trying to figure out how to get the result, take away the highest points and the lowest points and divide the result by 3 to get the average
????????????????????????????????????????????
Gonbe 32
I am not really sure what you mean by that. Could you elaborate and/or provide an example?
clubberlangMayo
for example;
the judges award the first competitor as follows;
judge1 enters 3,
judge2 enters 2.2,
judge3 enters 2,
judge4 enters 5,
judge5 enters 1.8,
the lowest points are 1.8
the highest are 5
the total is 14
the average would be (totalhighestlowest /3)
then this would give the average of 3 judges excluding the highest and lowest points awarded by 2 judges
Gonbe 32
You could try something like this:
#include <iostream>
#include <algorithm>
using namespace std;
const int AMOUNT_OF_GRADES = 5;
double Average (const double array[], const int count)
{
double result = 0;
for (int i = 0; i < count; i++)
{
result += array[i] / count;
}
return result;
}
double Sum (const double array[], const int count)
{
if (count <= 0)
{
return 0;
}
else
{
return array[0] + Sum(array + 1, count  1);
}
}
int main()
{
double grades[AMOUNT_OF_GRADES] = { 3, 2.2, 2, 5, 1.8 };
// Sort the array.
// The lowest will be at index 0 and the highest at index AMOUNT_OF_GRADES  1.
sort(grades, grades + 5);
cout << "Lowest: " << grades[0] << endl;
cout << "Highest: " << grades[AMOUNT_OF_GRADES  1] << endl;
cout << "Average: " << Average(grades + 1, AMOUNT_OF_GRADES  2) << endl;
cout << "Total: " << Sum(grades, AMOUNT_OF_GRADES) << endl;
return 0;
}
Let me know if anything is unclear.
edit
You could also define your average function like this:
double Average (const double array[], const int count)
{
return Sum(array, count) / count;
}
I didn't do that because this is more vulnerable to arithmetic overflow. (which is just theoretical in this case.. a sum of scores that is bigger than what a double can hold won't realistically happen)
I also defined AMOUNT_OF_GRADES while it's not needed. I thought it would be clearer. You could remove it and pass the array size as sizeof(grades)/sizeof(double)
.
edit
For completeness' sake:
#include <iostream>
#include <algorithm>
using namespace std;
double Sum (const double array[], const int count);
double Average (const double array[], const int count);
double Sum (const double array[], const int count)
{
if (count <= 0)
{
return 0;
}
else
{
return array[0] + Sum(array + 1, count  1);
}
}
double Average (const double array[], const int count)
{
return Sum(array, count) / count;
}
int main()
{
double grades[] = { 3, 2.2, 2, 5, 1.8 };
const int AMOUNT_OF_GRADES = sizeof(grades) / sizeof(double);
// Sort the array.
// The lowest will be at index 0 and the highest at index AMOUNT_OF_GRADES  1.
sort(grades, grades + AMOUNT_OF_GRADES);
if (AMOUNT_OF_GRADES > 0)
{
cout << "Lowest: " << grades[0] << endl;
cout << "Highest: " << grades[AMOUNT_OF_GRADES  1] << endl;
// No idea what the plan is for less than 3 scores, so just including everything when that happens.
if (AMOUNT_OF_GRADES >= 3)
cout << "Average: " << Average(grades + 1, AMOUNT_OF_GRADES  2) << endl;
else
cout << "Average: " << Average(grades, AMOUNT_OF_GRADES) << endl;
cout << "Total: " << Sum(grades, AMOUNT_OF_GRADES) << endl;
}
return 0;
}
Edited
by Gonbe
clubberlangMayo
thats brilliant thanks. could i ask you for slight bit more help?
see your original code up top^
how would you write a code to store the best result for all the competitors and
then show the winner at the very end?
ive learnt a great deal from your coding help and its greatly appreciated