Skip to content

Instantly share code, notes, and snippets.

@ereidland
Created April 13, 2013 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ereidland/5379255 to your computer and use it in GitHub Desktop.
Save ereidland/5379255 to your computer and use it in GitHub Desktop.
Example 7 at Library
//Question 12 answer
//Given this struct:
struct student
{
string name;
int credits;
double grades[6];
};
student * highestGrade(student students[], int numberOfStudents)
{
//Breaks if number of students is < 1, but the given input is going to be 41 students on the question.
//Null means nothing.
student * highest = NULL;
double highestAverage = 0;
for (int i = 0; i < numberOfStudents; i++)
{
//Get average.
double average = 0;
//Loop through all grades of students[i]
for (int j = 0; j < 6; j++)
{
average += students[i].grades[j];
}
average /= 6; //Get actual average.
//If average is better than the highest average.
if (average > highestAverage)
{
//Then make the current student the highest scoring one.
highest = &students[i]; //Pointer to student at i.
//Update highest average.
highestAverage = average;
}
}
//Done!
return highest;
}
//Input in question is 41 students.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment