C++ Problem: Program to compute, sort, and display student grades for a course P
ID: 3602574 • Letter: C
Question
C++ Problem: Program to compute, sort, and display student grades for a course
Problem B) (20 Points) Program to compute, sort, and display student grades for a course Information processing such as computing averages, sorting data and displaying the results is something that is used ubiquitously. Assume the course records for a particular course are in a file named: “Grades·txt" (this file will always serve as the input file for your program). The input file is in exactly the following format. Each line contains a student's x.500id, then one space, then five quiz scores. The quiz numbers can be real numbers (numbers that contain fractional components) and are each separated by one space For example, three entries in the file “Grades.txt', might contain the following data 01son38 95 95 9 93 85 kumar@1 9 85 85 95 92 For each student, your program should read in the data on each student from the file "Grades.txt", and compute their final average using their five quiz scores, and store it with each student's quiz scores. Note that a two- dimensional array, or parallel arrays might be useful in this regard. For example, after computing the averages for the three students above, your program will store (in memory, no need to write back to file) the following data for each of them 01son38 95 95 9 93 85 91.6 kumar@1 9 85 85 95 92 89.4 (So Ilux®6 has a Final Average of 89. , olson038 has a Final Average of 91. 6, and kumar01 has a final average of 89.4) After doing this, your program should enter a continuation loop that: Prompts the user if they would like to sort the results by X.500 id or final grade, and 2. 1. obtains the user's response 3. sorts the data according to the user's response; 4. displays the results to the console; 5. ask the user if they would like to continue A possible example of a run of your program is as follows Would you like to sort the results by x.500id or final grade (enter x or g)?: x kumar010 89.4 89 90 85 85 95 92 liux0006Explanation / Answer
I have answered this question earlier and giving you the code for same. Hope it helps.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
int loadFile(string filename, string id[], int scores[50][5] );
void calculateAverage(int scores[50][5], double average[50], int n);
void sortById(string id[], int scores[50][5], double average[50], int n);
void sortByGrade(string id[], int scores[50][5], double average[50], int n);
void show(string id[], int scores[50][5], double average[50], int n);
void swap(int i, int j, string id[], int scores[50][5], double average[50]);
int main()
{
int count;
//3 parallel arrays
string ids[50];
int scores[50][5];
double average[50];
string filename = "Grades.txt";
//reads from file and loads data into the ids and scores arrays
count = loadFile(filename, ids, scores);
calculateAverage(scores, average, count);
string choice = "y";
while(true)
{
cout << " Would you like to sort the result by x.500id or final grade (enter x or g) ? : ";
cin >> choice;
if(choice == "x" || choice == "X")
{
sortById(ids, scores, average, count);
show(ids, scores, average, count);
}
else if(choice == "g" || choice == "G")
{
sortByGrade(ids, scores, average, count);
show(ids, scores, average, count);
}
else
{
cout << "Error: please enter x for x.500id or g for final grade" << endl ;
continue;
}
cout << "Would you like to continue ? (enter y or Y): ";
cin >> choice;
if(choice != "y" && choice != "Y")
break;
}
}
int loadFile(string filename, string id[], int scores[50][5] )
{
ifstream ifs(filename.c_str());
if(!ifs.is_open())
{
cout << "Error opening input file : " << filename << endl;
exit(1);
}
int n = 0;
while(ifs >> id[n])
{
for(int i = 0; i < 5; i++)
ifs >> scores[n][i];
n++;
}
ifs.close();
return n;
}
void calculateAverage(int scores[50][5], double average[50], int n)
{
double avg = 0;
for(int i = 0; i < n; i++)
{
avg = 0;
for(int j = 0; j < 5; j++ )
avg += scores[i][j];
avg /= 5;
average[i] = avg;
}
}
void show(string id[], int scores[50][5], double average[50], int n)
{
for(int i = 0 ;i < n; i++)
{
cout << id[i] << " " ;
for(int j = 0; j < 5; j++)
cout << scores[i][j] << " ";
cout << average[i] << endl;
}
}
void sortById(string id[], int scores[50][5], double average[50], int n)
{
int i ,j, minIdx;
string temp;
for ( i = 0; i < n; ++i )
{
minIdx = i;
for(j = i+1; j < n; j++)
{
if( id[j] < id[minIdx])
minIdx = j;
}
if(i != minIdx)
{
swap(i, minIdx, id, scores, average);
}
}
}
void sortByGrade(string id[], int scores[50][5], double average[50], int n)
{
int i ,j, maxIdx;
string temp;
for ( i = 0; i < n; ++i )
{
maxIdx = i;
for(j = i+1; j < n; j++)
{
if( average[j] > average[maxIdx])
maxIdx = j;
}
if(i != maxIdx)
{
swap(i, maxIdx, id, scores, average);
}
}
}
void swap(int i, int j, string id[], int scores[50][5], double average[50])
{
string tempstr = id[i];
id[i] = id[j];
id[j] = tempstr;
int tempscore;
for(int k = 0; k < 5; k++)
{
tempscore = scores[i][k];
scores[i][k]=scores [j][k];
scores[j][k] = tempscore;
}
double tempavg = average[i];
average[i] = average[j];
average[j] = tempavg;
}
output
Would you like to sort the result by x.500id or final grade (enter x or g) ? : x
kumar010 90 85 85 95 92 89.4
liux0006 90 80 90 100 85 89
olson038 95 95 90 93 85 91.6
Would you like to continue ? (enter y or Y): y
Would you like to sort the result by x.500id or final grade (enter x or g) ? : g
olson038 95 95 90 93 85 91.6
kumar010 90 85 85 95 92 89.4
liux0006 90 80 90 100 85 89
Would you like to continue ? (enter y or Y): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.