Advertisement
riggnaros

Mindtap9_2

Feb 9th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct studentType {
  9.  
  10.     string studentFName;
  11.     string studentLName;
  12.     int testScore;
  13.     char grade;
  14. };
  15.  
  16. void getData(studentType students[]); //A function to read the students’ data into the array.
  17.  
  18. void assignGrade(studentType students[]); //A function to assign the relevant grade to each student.
  19.  
  20. int highScore(studentType students[], int& topGun, int n); //A function to find the highest test score.
  21.  
  22. void leaderboard(studentType students[], int& topGun, string& studentLeaderboard); //A function to print the names of the students having the highest test score.
  23.  
  24. void outputData(studentType students[], int& topGun, string& studentLeaderboard); //A function to print the data.
  25.  
  26. int main()
  27. {
  28.  
  29.     studentType students[20];
  30.     string studentLeaderboard;
  31.     int n = 20;
  32.     int topGun = 0;
  33.  
  34.     getData(students);
  35.     assignGrade(students);
  36.     highScore(students, topGun, n);
  37.     leaderboard(students, topGun, studentLeaderboard);
  38.     outputData(students, topGun, studentLeaderboard);
  39.  
  40.     return 0;
  41. };
  42.  
  43. void getData(studentType students[])
  44. {
  45.     ifstream infile;
  46.     infile.open("Ch9_Ex2Data.txt");
  47.  
  48.     for (int i = 0; i < 20; i++) {
  49.         infile >> students[i].studentFName
  50.             >> students[i].studentLName
  51.             >> students[i].testScore;
  52.     }
  53.     infile.close();
  54. };
  55.  
  56. void assignGrade(studentType students[])
  57. {
  58.  
  59.     for (int i = 0; i < 20; i++) {
  60.         if (students[i].testScore >= 92)
  61.             students[i].grade = 'A';
  62.         else if (students[i].testScore >= 85 && students[i].testScore < 92)
  63.             students[i].grade = 'B';
  64.         else if (students[i].testScore >= 78 && students[i].testScore < 85)
  65.             students[i].grade = 'C';
  66.         else if (students[i].testScore >= 70 && students[i].testScore < 78)
  67.             students[i].grade = 'D';
  68.         else if (students[i].testScore < 70)
  69.             students[i].grade = 'F';
  70.     }
  71. };
  72.  
  73. int highScore(studentType students[], int& topGun, int n)
  74. {
  75.     int pos_max;
  76.  
  77.     for (int i = 0; i < n - 1; i++) {
  78.         pos_max = i; //set pos_max to the current index of array
  79.  
  80.         for (int j = i + 1; j < n; j++) {
  81.  
  82.             if (students[j].testScore > students[pos_max].testScore)
  83.                 pos_max = j;
  84.             //pos_max will keep track of the index that max is in, this is needed when a swap happens
  85.         }
  86.  
  87.         //if pos_max no longer equals i then a larger value must have been found, so a swap must occur
  88.         if (pos_max != i) {
  89.             studentType temp = students[i];
  90.             students[i] = students[pos_max];
  91.             students[pos_max] = temp;
  92.         }
  93.     }
  94.     topGun = students[0].testScore;
  95.     return topGun;
  96. };
  97.  
  98. void leaderboard(studentType students[], int& topGun, string& studentLeaderboard)
  99.  
  100. {
  101.  
  102.     for (int i = 0; i < 20; i++) {
  103.  
  104.         if (students[i].testScore == topGun)
  105.             studentLeaderboard += students[i].studentFName + ' ' + students[i].studentLName + ',' + ' ';
  106.     }
  107. };
  108.  
  109. void outputData(studentType students[], int& topGun, string& studentLeaderboard)
  110. {
  111.     ofstream outfile;
  112.  
  113.     outfile.open("Ch9_Ex2Out.txt");
  114.  
  115.     for (int i = 0; i < 20; i++) {
  116.         outfile << students[i].studentLName
  117.                 << ", "
  118.                 << students[i].studentFName
  119.                 << ' '
  120.                 << students[i].testScore
  121.                 << ' '
  122.                 << students[i].grade
  123.                 << endl;
  124.     }
  125.  
  126.     outfile << "The highest test score is: " << topGun << endl;
  127.     outfile << "The student(s) who achieved this grade are: " << studentLeaderboard << endl;
  128.     outfile.close();
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement