Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- using namespace std;
- struct studentType {
- string studentFName;
- string studentLName;
- int testScore;
- char grade;
- };
- void getData(studentType students[]); //A function to read the students’ data into the array.
- void assignGrade(studentType students[]); //A function to assign the relevant grade to each student.
- int highScore(studentType students[], int& topGun, int n); //A function to find the highest test score.
- void leaderboard(studentType students[], int& topGun, string& studentLeaderboard); //A function to print the names of the students having the highest test score.
- void outputData(studentType students[], int& topGun, string& studentLeaderboard); //A function to print the data.
- int main()
- {
- studentType students[20];
- string studentLeaderboard;
- int n = 20;
- int topGun = 0;
- getData(students);
- assignGrade(students);
- highScore(students, topGun, n);
- leaderboard(students, topGun, studentLeaderboard);
- outputData(students, topGun, studentLeaderboard);
- return 0;
- };
- void getData(studentType students[])
- {
- ifstream infile;
- infile.open("Ch9_Ex2Data.txt");
- for (int i = 0; i < 20; i++) {
- infile >> students[i].studentFName
- >> students[i].studentLName
- >> students[i].testScore;
- }
- infile.close();
- };
- void assignGrade(studentType students[])
- {
- for (int i = 0; i < 20; i++) {
- if (students[i].testScore >= 92)
- students[i].grade = 'A';
- else if (students[i].testScore >= 85 && students[i].testScore < 92)
- students[i].grade = 'B';
- else if (students[i].testScore >= 78 && students[i].testScore < 85)
- students[i].grade = 'C';
- else if (students[i].testScore >= 70 && students[i].testScore < 78)
- students[i].grade = 'D';
- else if (students[i].testScore < 70)
- students[i].grade = 'F';
- }
- };
- int highScore(studentType students[], int& topGun, int n)
- {
- int pos_max;
- for (int i = 0; i < n - 1; i++) {
- pos_max = i; //set pos_max to the current index of array
- for (int j = i + 1; j < n; j++) {
- if (students[j].testScore > students[pos_max].testScore)
- pos_max = j;
- //pos_max will keep track of the index that max is in, this is needed when a swap happens
- }
- //if pos_max no longer equals i then a larger value must have been found, so a swap must occur
- if (pos_max != i) {
- studentType temp = students[i];
- students[i] = students[pos_max];
- students[pos_max] = temp;
- }
- }
- topGun = students[0].testScore;
- return topGun;
- };
- void leaderboard(studentType students[], int& topGun, string& studentLeaderboard)
- {
- for (int i = 0; i < 20; i++) {
- if (students[i].testScore == topGun)
- studentLeaderboard += students[i].studentFName + ' ' + students[i].studentLName + ',' + ' ';
- }
- };
- void outputData(studentType students[], int& topGun, string& studentLeaderboard)
- {
- ofstream outfile;
- outfile.open("Ch9_Ex2Out.txt");
- for (int i = 0; i < 20; i++) {
- outfile << students[i].studentLName
- << ", "
- << students[i].studentFName
- << ' '
- << students[i].testScore
- << ' '
- << students[i].grade
- << endl;
- }
- outfile << "The highest test score is: " << topGun << endl;
- outfile << "The student(s) who achieved this grade are: " << studentLeaderboard << endl;
- outfile.close();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement