Advertisement
Guest User

halp

a guest
May 31st, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. struct student
  8. {
  9.     string name;
  10.     int grade;
  11. };
  12.  
  13. void outputResults(student *students, int numberOfStudents)
  14. {
  15.     for (int current{}; current<numberOfStudents; ++current)
  16.         cout << students[current].name << " got a grade of " << students[current].grade << "\n";
  17. }
  18.  
  19. void sortStudents(student *studentsToSort, int numberOfStudents)
  20. {
  21.     for (int start{}; start>(numberOfStudents-1); ++start)
  22.     {
  23.         int highestScore, highestStudent;
  24.         for (int current{start+1}; current<numberOfStudents; ++current)
  25.         {
  26.             if ((studentsToSort[current].grade)>highestScore)
  27.             {
  28.                 highestScore=studentsToSort[current].grade;
  29.                 highestStudent=current;
  30.             }
  31.         }
  32.         swap(studentsToSort[start].grade, highestScore);
  33.         swap(studentsToSort[start].name, studentsToSort[highestStudent].name);
  34.     }
  35. }
  36.  
  37. int main()
  38. {
  39.     cout << "How many students do you wish to enter?: ";
  40.     int numberOfStudents;
  41.     cin >> numberOfStudents;
  42.     student *students = new student[numberOfStudents];
  43.  
  44.     for (int student{}; student<numberOfStudents; ++student)
  45.     {
  46.         cin >> students[student].name;
  47.         cin >> students[student].name;
  48.     }
  49.  
  50.     cout << '\n';
  51.     sortStudents(students, numberOfStudents);
  52.     outputResults(students, numberOfStudents);
  53.    
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement