Question
PROGRAM CODE (for reference): // Headers # include #include // cout, cin #include // exit() #include // strings #include // file processing #include // stream
PROGRAM CODE (for reference): // Headers # include#include // cout, cin #include // exit() #include // strings #include // file processing #include // stream manipulation using namespace std; // Global variables const int MAX_STUDENTS = 25; // We will not process more than 25 students even if the file contains more const int MAX_GRADES = 5; // Each student has exactly 5 grades const string FILENAME = "NamesGrades.txt"; // The name of the file that you will read // Function declarations int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents); void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount); void displayMax(string students[], int grades[][MAX_GRADES], int studentCount); void displayMin(string students[], int grades[][MAX_GRADES], int studentCount); char getLetterGrade(double grade); int getLongestNameLength(string students[], int studentCount); int main() { // You will need some variables here // You need one to keep up with the actual number of students // You need an array of strings for the student names // You need a two dimensional array of ints for the grades of the students // You need a variable to hold the choice of the user for the menu string students[MAX_STUDENTS]; // to hold students' first and last namea int grades[MAX_STUDENTS][MAX_GRADES]; // to hold students' grades int studentsCount = 0; // Get students and grades studentsCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS); int choice = 0; // Loop until user says to quit do { switch (choice) { case 1: displayAverages(students, grades, studentsCount); break; case 2: displayMax(students, grades, studentsCount); break; case 3: displayMin(students, grades, studentsCount); break; default: cout << "Grade Report Program" << endl; } // present menu and get user's choice cout << " \t1. Display Average Grade" << endl; cout << "\t2. Display Maximum Grade" << endl; cout << "\t3. Display Minimum Grade" << endl; cout << "\t4. Quit Program." << endl << endl; // ask to enter choice cout << "Enter your choice (1-4) : "; // Process the choice cin >> choice; } while (choice != 4); // End of program cout << "Thank you for using the program! " << endl; // Make sure we place the end message on a new line cout << endl; return 0; } /*********************************************************** loadStudentNameGrades opens and read fileName. It will read in two strings, concatenate them, and then save to the students array. It then reads five integers and save each to the grades array. The function will return the actual number of student/grade combinations read PARAM: students is an array of strings that can hold up ot maxStudents values grades is a two dimensional array for holding the grades of each student fileName is the name of the file that will be opened and read maxStudents is the maximum number of students that we will read from the file PRE: students[] is large enough to contain up to maxStudents elements grades[] is large enough ot contain up to maxStudents elements POST: students[] contains the names of up to maxStudents grades[][] contains the grades for up to maxStudents The number of student/grade combinations actually read from the file is returned. This value can range between 0 <= numStudents <= maxStudents NOTE: students[] and grades[] are meant to be parralel arrays. students[0] and grades[0] are the same student ************************************************************/ int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents) { ifstream input; input.open(fileName.c_str()); // to open the file string fname, lname; int g1, g2, g3, g4, g5; int index = 0; while (!input.eof() && index > fname >> lname >> g1 >> g2 >> g3 >> g4 >> g5; students[index] = fname + lname; grades[index][0] = g1; grades[index][1] = g2; grades[index][2] = g3; grades[index][3] = g4; grades[index][4] = g5; index++; } return index; // for stub out purposes, change this in your code input.close(); } /*********************************************************** displayAverages calculates the average of each student and displays the students name, average, and letter grade of the average in a table PARAM: students[] is an array of strings that contains the names of studentCount students grades[] is an array of integers that contains the grades of studentCount students studentCount contains the value of the number of elements in the students[] and grades[] arrays PRE: students[] and grades[] contain values for studentCount elements POST: table of student names, averages, and letter grades is displayed ************************************************************/ void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount) { // let get the grade average cout << " Grade Average" << endl; cout << "Name" << setw(30) << "Average Grade" << endl; for (int i = 0; i < studentCount; i++) { double sum = 0; for (int j = 0; j < MAX_GRADES; j++) { sum += grades[i][j]; } double avg = sum / MAX_GRADES; cout << students[i] << "\t\t" << avg << "\t" << getLetterGrade(avg) << endl; } } /*********************************************************** displayMax calculates the maximum grade of each student and displays the students name, maximum grade, and letter grade of the maximum grade in a table PARAM: students[] is an array of strings that contains the names of studentCount students grades[] is an array of integers that contains the grades of studentCount students studentCount contains the value of the number of elements in the students[] and grades[] arrays PRE: students[] and grades[] contain values for studentCount elements POST: table of student names, maximum grades, and letter grades is displayed ************************************************************/ void displayMax(string students[], int grades[][MAX_GRADES], int studentCount) { // get the maximum score of the student cout << " Grade Maximum" << endl; cout << "Name" << setw(30) << "Maximum Grade" << endl; for (int i = 0; i < studentCount; i++) { int max = 0; for (int j = 0; j < MAX_GRADES; j++) { // yeah, we got the max score if (grades[i][j] > max) max = grades[i][j]; } cout << students[i] << "\t\t" << max << "\t" << getLetterGrade(max) << endl; } } /*********************************************************** displayMin calculates the minimum grade of each student and displays the students name, minimum grade, and letter grade of the minimum grade in a table PARAM: students[] is an array of strings that contains the names of studentCount students grades[] is an array of integers that contains the grades of studentCount students studentCount contains the value of the number of elements in the students[] and grades[] arrays PRE: students[] and grades[] contain values for studentCount elements POST: table of student names, minimum grades, and letter grades is displayed ************************************************************/ void displayMin(string students[], int grades[][MAX_GRADES], int studentCount) { // let get the minimum score now cout << " Grade Minimum" << endl; cout << "Name" << setw(30) << "Minimum Grade" << endl; for (int i = 0; i < studentCount; i++) { int min = 100; for (int j = 0; j < MAX_GRADES; j++) { // awesome, we found the minum score if (grades[i][j] < min) min = grades[i][j]; } cout << students[i] << "\t\t" << min << "\t" << getLetterGrade(min) << endl; } } /*********************************************************** getLetterGrade converts a numerical grade to a letter grade PARAM: grade is the numerical grade to convert. Expected range is 0 <= grade <= 100 PRE: grade contains a value in the correct range POST: The corresponding letter grade of the numerical grade is returned ************************************************************/ char getLetterGrade(double grade) // to get the letter grade { if (grade >= 90) return 'A'; else if (grade >= 80) return 'B'; else if (grade >= 70) return 'C'; else if (grade >= 60) return 'D'; else return 'F'; } /*********************************************************** getLongestNameLength returns the length of the longest string from a list of strings PARAM: students[] is an array of strings that contains the name of students studentCount is the size of the students[] array PRE: students[] contains studentCount names POST: The length of the longest string in students[] is returned ************************************************************/ int getLongestNameLength(string students[], int studentCount) { int longestLength = 0; for (int i = 0; i < studentCount; i++) { if (students[i].size()>longestLength) longestLength = students[i].size(); } return longestLength; }
Exercise
Refactor the program code above. The output of this program should be exactly the same as the program code above except that the data will be sorted. The program should present a menu to the user asking if they would like display the average grade, maximum grade, or minimum grade. The program should then display the information in a table. The table should include the student name along with the chosen data (average, minimum, or maximum). In addition, the information should be sorted from high to low by the data selected.
Instead of using parallel arrays, you will need one vector of a struct of students. You can use the following struct:
struct Student
{
string name;
double average;
int max;
int min;
};
The program should do the following:
Load the names of students into a vector of Student struct. For each student calculate the average, maximum, and minimum grade and store that in the struct as well. The file used will be called NamesGrades.txt.
Continuously present a menu to the user given him/her the options of average, maximum, minimum, or quit. If the user chooses to quit, the program should end otherwise the presentation of the menu should continue.
Display a table of appropriate grades based on the users selection sorted by the appropriate grades.
Make sure you use good programming style. This includes commenting ALL your variables and commenting throughout your code. Comments should explain why you are doing something. Use good indentation. Make sure variable names are self-documenting. Make good use of white space. Group logical sections together with one blank line between logical sections.
Your output should be neat and pleasant to read.
Make sure you follow the specifications. If you must, you can add to the program, but do not change the specifications in doing so.
File NamesGrades.txt data
Sidra Amartey 90 88 70 74 70 Rebecca Brown 85 98 73 78 74 Leslie Carter 92 73 86 36 87 Ashley Guillen 95 26 90 83 85 Ryan Hilliard 75 66 69 100 52 Dawn Hopkins 84 69 66 88 74 Kyle Jiwani 7 99 96 84 89 Melvin Johnson 73 80 63 38 88 Edward Maun 82 85 72 75 99 Angelo Morrison 95 97 80 31 70 Michael Nguyen 71 70 91 93 28 Zack Nutt 82 85 97 74 98 Diana Patel 77 70 88 68 82 Patrick Perez 87 77 21 88 7 Abigail Peterson 64 81 75 85 70 Jennifer Putnam 39 91 85 80 70 Kimberly Sanjel 64 69 74 97 12 Marisa Santos 63 77 90 15 60 Hannah Shrestha 13 77 95 97 99 Linda Stoll 50 85 72 91 23 Victoria Taylor 95 93 74 63 90 Haily Wright 80 90 99 68 84
Below is possible output for a sample run for this programming assignment:
Grade Report Program
1. Display Average Grade
2. Display Maximum Grade
3. Display Minimum Grade
4. Quit Program
Enter your choice (1-4): 1
Grade Averages
Name Average Grade
Zack Nutt 87.2 B
Haily Wright 84.2 B
Victoria Taylor 83.0 B
Edward Maun 82.6 B
Rebecca Brown 81.6 B
Sidra Amartey 78.4 C
Diana Patel 77.0 C
Dawn Hopkins 76.2 C
Hannah Shrestha 76.2 C
Ashley Guillen 75.8 C
Kyle Jiwani 75.0 C
Abigail Peterson 75.0 C
Leslie Carter 74.8 C
Angelo Morrison 74.6 C
Jennifer Putnam 73.0 C
Ryan Hilliard 72.4 C
Michael Nguyen 70.6 C
Melvin Johnson 68.4 D
Linda Stoll 64.2 D
Kimberly Sanjel 63.2 D
Marisa Santos 61.0 D
Patrick Perez 56.0 F
Press any key to continue . . .
Grade Report Program
1. Display Average Grade
2. Display Maximum Grade
3. Display Minimum Grade
4. Quit Program
Enter your choice (1-4): 2
Max Grades
Name Max Grade
Ryan Hilliard 100 A
Kyle Jiwani 99 A
Edward Maun 99 A
Hannah Shrestha 99 A
Haily Wright 99 A
Rebecca Brown 98 A
Zack Nutt 98 A
Angelo Morrison 97 A
Kimberly Sanjel 97 A
Ashley Guillen 95 A
Victoria Taylor 95 A
Michael Nguyen 93 A
Leslie Carter 92 A
Jennifer Putnam 91 A
Linda Stoll 91 A
Sidra Amartey 90 A
Marisa Santos 90 A
Dawn Hopkins 88 B
Melvin Johnson 88 B
Diana Patel 88 B
Patrick Perez 88 B
Abigail Peterson 85 B
Press any key to continue . . .
Grade Report Program
1. Display Average Grade
2. Display Maximum Grade
3. Display Minimum Grade
4. Quit Program
Enter your choice (1-4): 3
Min Grades
Name Min Grade
Zack Nutt 74 C
Rebecca Brown 73 C
Edward Maun 72 C
Sidra Amartey 70 C
Diana Patel 68 D
Haily Wright 68 D
Dawn Hopkins 66 D
Abigail Peterson 64 D
Victoria Taylor 63 D
Ryan Hilliard 52 F
Jennifer Putnam 39 F
Melvin Johnson 38 F
Leslie Carter 36 F
Angelo Morrison 31 F
Michael Nguyen 28 F
Ashley Guillen 26 F
Linda Stoll 23 F
Marisa Santos 15 F
Hannah Shrestha 13 F
Kimberly Sanjel 12 F
Kyle Jiwani 7 F
Patrick Perez 7 F
Press any key to continue . . .
Grade Report Program
1. Display Average Grade
2. Display Maximum Grade
3. Display Minimum Grade
4. Quit Program
Enter your choice (1-4): 4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started