Created
April 13, 2013 17:17
-
-
Save ereidland/5379255 to your computer and use it in GitHub Desktop.
Example 7 at Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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