In: Computer Science
On Lab 10 you did Chapter 8’s Programming Exercise 13, using the name Lab10TestScores and assuming that the class has ten students and five tests. Now let’s rewrite it to use dynamic arrays so that it will work for any number of students and any number of tests. Call this new program Lab11TestScores. Modify the program so that it asks the user to enter the number of students and the number of tests before it reads the data from the file named Lab11ScoresData.txt. (Two data files, Lab11ScoresData_5_3.txt and Lab11ScoresData_11_6.txt, are provided so you can use to test your program.). Let me know if you have questions. #include #include using namespace std; void readFile(string names[10], int scores[][5]) { ifstream in("Lab10ScoresData.txt"); for (int i = 0; i < 10; i++) { in >> names[i] >> scores[i][0] >> scores[i][1] >> scores[i][2] >> scores[i][3] >> scores[i][4]; } } void average(int scores[][5], float avg[], char grade[]) { int total; for (int i = 0; i < 10; i++) { total = 0; for (int j = 0; j < 5; j++) total += scores[i][j]; avg[i] = (float)total / (float)5; } for (int i = 0; i < 10; i++) { if (avg[i] >= 90) grade[i] = 'A'; if (avg[i] < 90 && avg[i] >= 80) grade[i] = 'B'; if (avg[i] < 80 && avg[i] >= 70) grade[i] = 'C'; if (avg[i] < 70 && avg[i] >= 60) grade[i] = 'D'; if (avg[i] < 60 && avg[i] >= 50) grade[i] = 'E'; if (avg[i] < 50) grade[i] = 'F'; } } void sortByName(string names[], float avg[], char grade[]) { for (int i = 0; i < 9; i++) { for (int j = i + 1; j < 10; j++) { if (names[i] > names[j]) { string n = names[i]; names[i] = names[j]; names[j] = n; float a = avg[i]; avg[i] = avg[j]; avg[j] = a; char g = grade[i]; grade[i] = grade[j]; grade[j] = g; } } } } void output(string names[], float avg[], char grade[]) { cout << "Name\tAverage\tGrade\n"; for (int i = 0; i < 10; i++) { cout << names[i] << "\t" << avg[i] << "\t" << grade[i] << endl; } } void countGrade(char grade[]) { int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0; cout << "\n\nGrade\tCount\n"; for (int i = 0; i < 10; i++) { if (grade[i] == 'A') a++; if (grade[i] == 'B') b++; if (grade[i] == 'C') c++; if (grade[i] == 'D') d++; if (grade[i] == 'E') e++; if (grade[i] == 'F') f++; } cout << "A\t" << a << endl; cout << "B\t" << b << endl; cout << "C\t" << c << endl; cout << "D\t" << d << endl; cout << "E\t" << e << endl; cout << "F\t" << f << endl; } int main() { string names[10]; int scores[10][5]; float avg[10]; char grade[10]; readFile(names, scores); average(scores, avg, grade); sortByName(names, avg, grade); output(names, avg, grade); countGrade(grade); system(" pause "); return 0; }
Lab11ScoresData_11_6.txt.
Johnson 85 83 77 91 76 99 Aniston 80 90 95 93 48 100 Cooper 78 81 11 90 73 54 Gupta 92 83 30 69 87 78 Blair 23 45 96 38 59 82 Clark 60 85 45 39 67 67 Kennedy 77 31 52 74 83 77 Bronson 93 94 89 77 97 80 Sunny 79 85 28 93 82 45 Smith 85 72 49 75 63 92 Groot 90 87 94 75 100 100
Lab11ScoresData_5_3.txt
Johnson 85 83 77 Aniston 80 90 95 Cooper 78 81 11 Gupta 92 83 30 Blair 23 45 96
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream>
using namespace std;
void readFile(string *names, int **scores, int num_studs, int num_tests)
{
ifstream in("Lab11ScoresData.txt");
for (int i = 0; i < num_studs; i++)
{
in >> names[i];
for(int j=0; j<num_tests; j++)
in>>scores[i][j];
}
}
void average(int **scores, int num_studs, int num_tests, float *avg, char *grade)
{
int total;
for (int i = 0; i < num_studs; i++)
{
total = 0;
for (int j = 0; j < num_tests; j++)
total += scores[i][j];
avg[i] = (float)total / (float)num_tests;
}
for (int i = 0; i < num_studs; i++)
{
if (avg[i] >= 90)
grade[i] = 'A';
if (avg[i] < 90 && avg[i] >= 80)
grade[i] = 'B';
if (avg[i] < 80 && avg[i] >= 70)
grade[i] = 'C';
if (avg[i] < 70 && avg[i] >= 60)
grade[i] = 'D';
if (avg[i] < 60 && avg[i] >= 50)
grade[i] = 'E';
if (avg[i] < 50)
grade[i] = 'F';
}
}
void sortByName(string *names, int num_studs, float *avg, char *grade)
{
for (int i = 0; i < num_studs-1; i++)
{
for (int j = i + 1; j < num_studs; j++)
{
if (names[i] > names[j])
{
string n = names[i];
names[i] = names[j];
names[j] = n;
float a = avg[i];
avg[i] = avg[j];
avg[j] = a;
char g = grade[i];
grade[i] = grade[j];
grade[j] = g;
}
}
}
}
void output(string *names, int num_studs, float *avg, char *grade)
{
cout << "Name\tAverage\tGrade\n";
for (int i = 0; i < num_studs; i++)
{
cout << names[i] << "\t" << avg[i] << "\t" << grade[i] << endl;
}
}
void countGrade(char *grade, int num_studs)
{
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
cout << "\n\nGrade\tCount\n";
for (int i = 0; i < 10; i++)
{
if (grade[i] == 'A')
a++;
if (grade[i] == 'B')
b++;
if (grade[i] == 'C')
c++;
if (grade[i] == 'D')
d++;
if (grade[i] == 'E')
e++;
if (grade[i] == 'F')
f++;
}
cout << "A\t" << a << endl;
cout << "B\t" << b << endl;
cout << "C\t" << c << endl;
cout << "D\t" << d << endl;
cout << "E\t" << e << endl;
cout << "F\t" << f << endl;
}
int main()
{
int num_studs, num_tests;
cout<<"Enter number of students: ";
cin>>num_studs;
cout<<"Enter number of tests: ";
cin>>num_tests;
string *names = new string[num_studs];
int **scores = new int*[num_studs];
for(int i=0; i<num_studs; i++)
scores[i] = new int[num_tests];
float *avg = new float[num_studs];
char *grade = new char[num_studs];
readFile(names, scores, num_studs,num_tests);
average(scores, num_studs, num_tests, avg, grade);
sortByName(names, num_studs, avg, grade);
output(names, num_studs, avg, grade);
countGrade(grade, num_studs);
// system(" pause ");
return 0;
}