fork download
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. struct Student
  7. {
  8. std::string name; //name of student
  9. double average; //average grades for student
  10. int max; //holds max grade for student
  11. int min; //holds min grade for student
  12. Student() : max(0), min(0), average(0) {}
  13. };
  14.  
  15. const int MAX_STUDENTS = 25;
  16. const int MAX_GRADES = 3;
  17.  
  18. Student studentNum[MAX_STUDENTS];
  19. int grades[MAX_STUDENTS][MAX_GRADES];
  20.  
  21. using namespace std;
  22.  
  23. void displayMax(Student* students, int grades[][MAX_GRADES], int studentCount)
  24. {
  25. //get max grade for each student
  26. for (int i = 0; i < studentCount; i++)
  27. students[i].max = *std::max_element(&grades[i][0], &grades[i][MAX_GRADES]);
  28.  
  29. // sort students based on max
  30. std::sort(students, students + studentCount, [](const Student& s1, const Student& s2)
  31. { return s1.max > s2.max;});
  32.  
  33. // output results
  34. cout << "\n";
  35. for (int i = 0; i < studentCount; ++i)
  36. cout << students[i].name << " " << students[i].max << "\n";
  37. }
  38.  
  39. int main()
  40. {
  41. studentNum[0].name = "Joe";
  42. grades[0][0] = 50;
  43. grades[0][1] = 100;
  44. grades[0][2] = 86;
  45.  
  46. studentNum[1].name = "Jack";
  47. grades[1][0] = 70;
  48. grades[1][1] = 80;
  49. grades[1][2] = 95;
  50.  
  51. studentNum[2].name = "Jill";
  52. grades[2][0] = 75;
  53. grades[2][1] = 95;
  54. grades[2][2] = 50;
  55.  
  56. displayMax(studentNum, grades, 3);
  57. }
  58.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Joe  100
Jack  95
Jill  95