Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you will refactor the program that you wrote for Assignment

ID: 3920025 • Letter: I

Question

In this assignment, you will refactor the program that you wrote for Assignment 3. The output of this program should be exactly the same as Assignment 3 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 fName; string lName; 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 user’s 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 (see my examples and the books examples for demonstration of 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.

// 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 it'll 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()

{

int studentCount = 0; // Number of students processing

int grades[MAX_STUDENTS][MAX_GRADES]; // Two dimensional array: Table of grades for students

string students[MAX_STUDENTS]; // Array: List of student's name

char choice; // Choice for user menu

// Get students and grades

studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);

// Loop until user says to quit

do

{

// present menu and get user's choice

cout << " Student Grade Report Program ";

cout << " 1. Display Average Grade "

<< " 2. Display Maximum Grade "

<< " 3. Display Minimum Grade "

<< " 4. Quit Program ";

cout << "Enter your choice (1-4): ";

cin >> choice;

while (getchar() != ' ');

// Process the choice

switch (choice)

{

case '1': // Average

displayAverages(students, grades, studentCount);

break;

case '2': // Maximum

displayMax(students, grades, studentCount);

break;

case '3': // Minimum

displayMin(students, grades, studentCount);

break;

case '4': // Quit

break;

default:

cout << "Invalid option. Please try again. ";

}

if (choice != '4')

{

cout << endl;

system("PAUSE");

system("CLS");

}

} while (choice != '4'); // End of do while loop

// End of program

// Make sure we place the end message on a new line

cout << endl;

system("PAUSE");

return 0;

}

int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents)

{

ifstream inFile; // Input File Stream

string studentName, // Name of student

studentLastName; // Last name of student

int numStudents = 0; // Number of students actually read

// Open the file

inFile.open(fileName);

if (!inFile)

{

cout << "File not found " << fileName << endl;

system("PAUSE");

exit(EXIT_FAILURE);

}

// Loop through each row

for (int i = 0;

i < maxStudents && (inFile >> studentName >> studentLastName);

i++, numStudents++)

{

// Loop through all the grades of each row

for (int j = 0; j < MAX_GRADES; j++)

{

inFile >> grades[i][j];

}

// Combine the student's name with the student's last name

students[i] = studentName + " " + studentLastName;

}

// Close the file

inFile.close();

return numStudents;

}

void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)

{

double average; // Average grade if all student's grades

int total; // Total of all grades (Accumulator)

// Get the longest name

int maxLength = getLongestNameLength(students, studentCount); // Longest name

// Set up Table header

cout << setprecision(1) << fixed << showpoint;

cout << " Grade Averages ";

cout << setw(maxLength + 1) << left << "Name"

<< setw(8) << right << "Average"

<< setw(8) << "Grade" << endl;

// Students

for (int i = 0; i < studentCount; i++)

{

cout << setw(maxLength + 1) << left << students[i];

total = 0; // Zeros out accumulator

// Process the grades

for (int j = 0; j < MAX_GRADES; j++)

{

total += grades[i][j];

}

average = static_cast<double>(total) / MAX_GRADES;

cout << setw(8) << right << average

<< setw(8) << getLetterGrade(average)

<< endl;

}

}

void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)

{

int maxGrade; // Max grade so far

int maxLength = getLongestNameLength(students, studentCount); // Longest name

// Header

cout << " Max grades ";

cout << setw(maxLength + 1) << left << "Name"

<< setw(4) << right << "Max"

<< setw(8) << "Grade" << endl;

// Max Grades

for (int i = 0; i < studentCount; i++)

{

cout << setw(maxLength + 1) << left << students[i];

maxGrade = grades[i][0]; // Initial Grade

for (int j = 0; j < MAX_GRADES; j++)

{

if (grades[i][j] > maxGrade)

{

maxGrade = grades[i][j];

}

}

cout << setw(4) << right << maxGrade

<< setw(8) << getLetterGrade(maxGrade)

<< endl;

}

}

void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)

{

int minGrade; // Max grade so far

int maxLength = getLongestNameLength(students, studentCount); // Longest name

// Set up the header

cout << " Min grades ";

cout << setw(maxLength + 1) << left << "Name"

<< setw(4) << right << "Min"

<< setw(8) << "Grade" << endl;

// Min Grades

for (int i = 0; i < studentCount; i++)

{

cout << setw(maxLength + 1) << left << students[i];

minGrade = grades[i][0]; // Initial Grade

for (int j = 0; j < MAX_GRADES; j++)

{

if (grades[i][j] < minGrade)

{

minGrade = grades[i][j];

}

}

cout << setw(4) << right << minGrade

<< setw(8) << getLetterGrade(minGrade)

<< endl;

}

}

char getLetterGrade(double grade)

{

if (grade >= 90.0)

return 'A';

else if (grade >= 80.0)

return 'B';

else if (grade >= 70.0)

return 'C';

else if (grade >= 60.0)

return 'D';

else

return 'F';

}

int getLongestNameLength(string students[], int studentCount)

{

int maxLength = 0; // Max length so far

for (int i = 0; i < studentCount; i++)

{

// Is it longer

if (students[i].length() > maxLength)

{

maxLength = students[i].length();

}

}

return maxLength;

}

Has to be written in c++ and compile in visual studio 2017

Explanation / Answer

// Header files

#include<iostream>
#include<iomanip>
#include<string>
#include <fstream>
#include <vector>
#include<algorithm>
#include<stdlib.h>
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

// Structure Student definition
typedef struct
{
// Structure member to store data
string name;
double average;
int max;
int min;
}Student; // End of structure


// Function declarations
void loadStudentNamesGrades(vector<Student> &, string fileName, int maxStudents);
void displayAverages(vector<Student>);
void displayMax(vector<Student>);
void displayMin(vector<Student>);
char getLetterGrade(double grade);
int getLongestNameLength(vector<Student>);

// main function definition
int main()
{
// Creates a vector object studentRec to store Student class object
std::vector<Student> studentRec;
// You will need some variables here
// You need one to keep up with the actual number of students
int studentCount = 0;
// You need a variable to hold the choice of the user for the menu
int menu;

// Read the contents of the file and stores it in Student vector
loadStudentNamesGrades(studentRec, FILENAME, MAX_STUDENTS);
// Present menu and get user's choice
const int average = 1,
max = 2,
min = 3,
quit = 4;
// Loops till user choice is not 4
do
{
// Displays menu
cout << " Grade Report Menu "
<< "1) Display Average Grade "
<< "2) Display Maximum Grade "
<< "3) Display Minimum Grade "
<< "4) Quit Program "
<< " Enter choice (1-4): ";
cin >> menu;
// Process the choice
switch (menu)
{
case average:
{
//Display Average
displayAverages(studentRec);
}
break;
case max:
{
//Display Maximum
displayMax(studentRec);
}
break;
case min:
{
//Display Minimum
displayMin(studentRec);
}
break;
}
} while (menu != quit);
// End of program
// Make sure we place the end message on a new line
cin.ignore();
cout << "Press ENTER key to exit";
cin.get();
return 0;
}// End of main

// Function to read the contents of file and stores it in Student vector
void loadStudentNamesGrades(vector<Student> &stuRec, string fileName, int maxStudents)
{
// Creates a temporary object
Student stuTemp;
// Input file stream
ifstream nameGrades;
// Name of the student and Letter Grade
string studentsFirstName, studentsLastName;
// Students actually read
int numStudents = 0;
// Open the file
nameGrades.open(fileName.c_str());
double marks[MAX_GRADES];
double total, avg;
double minimum, maximum;
// If file fails to open show error message
if (nameGrades.fail())
{
cin.ignore();
cout << "Error: Unable to open the file"<<fileName<<" Press ENTER key to exit";
cin.get();
exit(0);
}// End of if condition
// Loops till end of file
while(!nameGrades.eof())
{
// Reads the data and stores in Student object
// Read names
nameGrades>>studentsFirstName;
nameGrades>>studentsLastName;
// Concatenate names
stuTemp.name = studentsFirstName + " " + studentsLastName;
// Sets the initial value for total, minimum, and maximum
total = 0;
minimum = 101;
maximum = 0;
// Loops till maxGrades
for (int j = 0; j < MAX_GRADES; j++)
{
nameGrades >>marks[j];
// Calculates the total
total += marks[j];
// Finds the minimum grade
// If the current index position mark is less than the earlier minimum mark
if(marks[j] < minimum)
// Update the minimum marks with current mark
minimum = marks[j];
// Finds the maximum grade
// If the current index position mark is greater than the earlier maximum mark
if(marks[j] > maximum)
// Update the maximum marks with current mark
maximum = marks[j];
}// End of inner for loop
// Calculates average
avg = total / MAX_GRADES;
// Stores the average, maximum and minimum in the data member of the object
stuTemp.average = avg;
stuTemp.max = maximum;
stuTemp.min = minimum;

// Adds the object to vector
stuRec.push_back(stuTemp);
}// End of outer for loop
// Close the file
nameGrades.close();
}// End of function

// Function to sort the vector using bubble sort
// Parameter: Two objects of Student structure type
// Parameter: Type 1 for average, Type 2 for maximum grade, and Type 3 for minimum grade
void bubbleSort(vector<Student> &stuRec, int type)
{
Student temp;
//Loops till length of the vector
for(int x = 0; x < stuRec.size(); x++)
{
//Loops till length of the vector minus x minus one times
for(int y = 0; y < stuRec.size() - x - 1; y++)
{
// Checks if type is one then sort on average
if(type == 1)
{
//Checks if current index position of the vector average is less than the next index position of the vector average
if(stuRec[y].average < stuRec[y + 1].average)
{
// Swapping process
temp = stuRec[y];

stuRec[y] = stuRec[y + 1];
stuRec[y + 1] = temp;
}// End of inner if condition
}// End of outer if condition

// Checks if type is one then sort on maximum grade
if(type == 2)
{
//Checks if current index position of the vector maximum grade is less than the next index position of the vector maximum grade
if(stuRec[y].max < stuRec[y + 1].max)
{
// Swapping process
temp = stuRec[y];
stuRec[y] = stuRec[y + 1];
stuRec[y + 1] = temp;
}// End of inner if condition
}// End of outer if condition

// Checks if type is one then sort on minimum grade
if(type == 3)
{
//Checks if current index position of the vector minimum grade is less than the next index position of the vector minimum grade
if(stuRec[y].min < stuRec[y + 1].min)
{
// Swapping process
temp = stuRec[y];
stuRec[y] = stuRec[y + 1];
stuRec[y + 1] = temp;
}// End of inner if condition
}// End of outer if condition
}//End of inner for loop
}//End of outer for loop
}
// Displays the student name, average and grade in tabular format in descending order of average
// Parameter: Student vector
void displayAverages(vector<Student> stuRec)
{
// Calls the function to return the longest name length
int maxLength = getLongestNameLength(stuRec);
// Displays the heading
cout << " Student Record - Average Grade: ";
cout << setw(maxLength + 3) << left << "Name" << setw(9) << right << "Average" << setw(10) << "Grade" << endl;

// Calls the function compareAvg to compare the average mark
// Sort method to sort the Student vector based on the return result of the compareAvg
bubbleSort(stuRec, 1);
// Loops till end of the vector
for(int c = 0; c < stuRec.size(); c++)
{
// Displays student information
cout << setw(maxLength + 3) << left << stuRec[c].name;
cout << setprecision(2)<<setw(8) << std::right <<fixed<<stuRec[c].average << setw(9) << getLetterGrade(stuRec[c].average) << endl;
}// End of for loop
}// End of function

// Displays the student name, maximum mark and grade in tabular format in descending order of maximum mark
// Parameter: Student vector
void displayMax(vector<Student> stuRec)
{
// Calls the function to return the longest name length
int maxLength = getLongestNameLength(stuRec);
// Displays the heading
cout << " Student Record - Maximum Grade: ";
cout << setw(maxLength + 3) << left << "Name" << setw(9) << right << "Max" << setw(10) << "Grade" << endl;

// Calls the function compareMax to compare the student maximum mark
// sort method to sort the vector based on the return result of the compareMax
bubbleSort(stuRec, 2);
// Loops till end of the vector
for(int c = 0; c < stuRec.size(); c++)
{
// Displays student information
cout << setw(maxLength + 3) << left << stuRec[c].name;
cout << setprecision(2)<<setw(8) << std::right <<fixed<<stuRec[c].max << setw(9) << getLetterGrade(stuRec[c].average) << endl;
}// End of for loop
}// End of function

// Displays the student name, minimum mark and grade in tabular format in descending order of minimum mark
// Parameter: Student vector
void displayMin(vector<Student> stuRec)
{
// Calls the function to return the longest name length
int maxLength = getLongestNameLength(stuRec);
// Displays the heading
cout << " Student Record - Average Grade: ";
cout << setw(maxLength + 3) << left << "Name" << setw(9) << right << "Min" << setw(10) << "Grade" << endl;

// Calls the function compareMin to compare the student minimum mark
// sort method to sort the vector based on the return result of the compareMin
bubbleSort(stuRec, 3);
// Loops till end of the vector
for(int c = 0; c < stuRec.size(); c++)
{
// Displays student information
cout << setw(maxLength + 3) << left << stuRec[c].name;
cout << setprecision(2)<<setw(8) << std::right <<fixed<<stuRec[c].min << setw(9) << getLetterGrade(stuRec[c].average) << endl;
}// End of for loop
}// End of function

// Function to calculate student grade
// Parameter: average grade of type double of a student
// Return: character grade of a student
char getLetterGrade(double avgGrade)
{
// Checks the student avgGrade marks and return student grade in capital letter character form
if (avgGrade > 90)
return 65;
else if (avgGrade > 80)
return 66;
else if (avgGrade > 70)
return 67;
else if (avgGrade > 60)
return 68;
else if (avgGrade > 50)
return 70;
else if (avgGrade < 49)
return 70;
}// End of function

// Function to return the biggest length name of a student
// Parameter: Student vector
int getLongestNameLength(vector<Student> stuRec)
{
// To store maximum length of a name
int maxLength = 0;
// Loops till end of the vector
for(int c = 0; c < stuRec.size(); c++)
{
// Checks if current student name length is greater than current maximum length
if (stuRec[c].name.length() > maxLength)
// Updates maximum length with current student name length
maxLength = stuRec[c].name.length();
}// End of for loop
// Returns the maximum length
return maxLength;
}// End of function

Sample Run:

Grade Report Menu

1) Display Average Grade
2) Display Maximum Grade
3) Display Minimum Grade
4) Quit Program

Enter choice (1-4): 1

Student Record - Average Grade:
Name Average Grade
Zack Nutt 87.20 B
Haily Wright 84.20 B
Victoria Taylor 83.00 B
Edward Maun 82.60 B
Rebecca Brown 81.60 B
Sidra Amartey 78.40 C
Diana Patel 77.00 C
Dawn Hopkins 76.20 C
Hannah Shrestha 76.20 C
Ashley Guillen 75.80 C
Kyle Jiwani 75.00 C
Abigail Peterson 75.00 C
Leslie Carter 74.80 C
Angelo Morrison 74.60 C
Jennifer Putnam 73.00 C
Ryan Hilliard 72.40 C
Michael Nguyen 70.60 C
Melvin Johnson 68.40 D
Linda Stoll 64.20 D
Kimberly Sanjel 63.20 D
Marisa Santos 61.00 D
Patrick Perez 56.00 F

Grade Report Menu

1) Display Average Grade
2) Display Maximum Grade
3) Display Minimum Grade
4) Quit Program

Enter choice (1-4): 2

Student Record - Maximum Grade:
Name Max Grade
Ryan Hilliard 100 C
Kyle Jiwani 99 C
Edward Maun 99 B
Hannah Shrestha 99 C
Haily Wright 99 B
Rebecca Brown 98 B
Zack Nutt 98 B
Angelo Morrison 97 C
Kimberly Sanjel 97 D
Ashley Guillen 95 C
Victoria Taylor 95 B
Michael Nguyen 93 C
Leslie Carter 92 C
Jennifer Putnam 91 C
Linda Stoll 91 D
Sidra Amartey 90 C
Marisa Santos 90 D
Dawn Hopkins 88 C
Melvin Johnson 88 D
Diana Patel 88 C
Patrick Perez 88 F
Abigail Peterson 85 C

Grade Report Menu

1) Display Average Grade
2) Display Maximum Grade
3) Display Minimum Grade
4) Quit Program

Enter choice (1-4): 3

Student Record - Average Grade:
Name Min Grade
Zack Nutt 74 B
Rebecca Brown 73 B
Edward Maun 72 B
Sidra Amartey 70 C
Diana Patel 68 C
Haily Wright 68 B
Dawn Hopkins 66 C
Abigail Peterson 64 C
Victoria Taylor 63 B
Ryan Hilliard 52 C
Jennifer Putnam 39 C
Melvin Johnson 38 D
Leslie Carter 36 C
Angelo Morrison 31 C
Michael Nguyen 28 C
Ashley Guillen 26 C
Linda Stoll 23 D
Marisa Santos 15 D
Hannah Shrestha 13 C
Kimberly Sanjel 12 D
Kyle Jiwani 7 C
Patrick Perez 7 F

Grade Report Menu

1) Display Average Grade
2) Display Maximum Grade
3) Display Minimum Grade
4) Quit Program

Enter choice (1-4): 4
Press ENTER key to exit