Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. /*
  2. * Obenauf
  3. * 19-01-18
  4. */
  5.  
  6. #include <iostream>
  7. #include <string>
  8.  
  9. struct students
  10. {
  11.     double score;
  12.     std::string name;
  13. };
  14.  
  15. void sort(struct students *studentsptr, int);
  16. void display(struct students *studentsptr, int);
  17.  
  18. int main()
  19. {
  20.     students *studentsptr = nullptr;
  21.     int average, numscores, total = 0;
  22.  
  23.     std::cout << "How many tests to input? ";
  24.     std::cin >> numscores;
  25.     studentsptr = new students[numscores];
  26.  
  27.     std::cout << "Enter name and score seperated by a space. \n";
  28.  
  29.     for (int i = 0; i < numscores; i++)
  30.         std::cin >> studentsptr[i].name >> studentsptr[i].score;
  31.    
  32.     sort(studentsptr, numscores);
  33.    
  34.     std::cout << "The sorted results are: \n";
  35.     display(studentsptr, numscores);
  36.    
  37.     for (int j = 0; j < numscores; j++)
  38.         total += studentsptr[j].score;
  39.     average = total / numscores;
  40.    
  41.     std::cout << "The average score is: " << average << std::endl; //displays score average
  42.     delete[]studentsptr;
  43.     studentsptr = nullptr;
  44.     return 0;
  45. }
  46.  
  47. void sort(students *studentsptr, int n)
  48. {
  49.     int i, j, minidx, tempi;
  50.     std::string temps;
  51.  
  52.     for (i = 0; i < n - 1; i++)
  53.     {
  54.         minidx = i;
  55.         for (j = i + 1; j < n; j++)
  56.             if (studentsptr[j].score > studentsptr[minidx].score)
  57.                 minidx = j;
  58.         //swaps score
  59.         tempi = studentsptr[minidx].score;
  60.         studentsptr[minidx].score = studentsptr[i].score;
  61.         studentsptr[i].score = tempi;
  62.         //swaps name
  63.         temps = studentsptr[minidx].name;
  64.         studentsptr[minidx].name = studentsptr[i].name;
  65.         studentsptr[i].name = temps;
  66.     }
  67. }
  68.  
  69. void display(students *studentsptr, int n)
  70. {
  71.     for (int i = 0; i < n; i++)
  72.         std:: cout << studentsptr[i].name << " " << studentsptr[i].score << std::endl;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement