Modify the Test Scores program from the above Debug Section into a class grade b
ID: 3559553 • Letter: M
Question
Modify the Test Scores program from the above Debug Section into a class grade book that includes a class of up to 20 students and 3 test grades. Each test is read in as a separate file, so 3 Test files are required. The students grades should be kept in an array. Once all three test scores are read in, calculate each students average and grade for the class. Also print a report of the each students test grades, average and final grade as a table, and then print the averages of each Test and overall Test Average, as well as highest Test Grade of all Tests and the student with the highest class grade.
#include <iostream>
#include <iomanip>
#include<fstream>
using namespace std;
void readData(ifstream& inputFile, int list[], int size);
void holdscrn( ); // void function to hold screen open before exit
void print(int list[], int size);
int main()
{
int scores[8] = {0};
ifstream infile;
infile.open("TestScoresData.txt");
if (!infile)
{
cout << "Cannot open the input file. Program terminates!"
<< endl;
holdscrn( ); // Hold screen before exit
return 1;
}
readData(infile, scores, 8);
print(scores, 8);
cout << endl;
infile.close();
holdscrn( ); // Hold screen before exit
return 0;
}
void readData(ifstream& inputFile, int list[], int size)
{
int score;
int index;
inputFile >> score;
while (inputFile)
{
index = score / 25;
if (index == size)
index--;
if (index < size)
list[index]++;
inputFile >> score;
}
}
void print(int list[], int size)
{
int range;
int lowRange = 0;
int upperRange = 24;
cout << " Range # of Students" << endl;
for (range = 0; range < size; range++)
{
cout << setw(3) << lowRange << " - "
<< upperRange << setw(15)
<< list[range] << endl;
lowRange = upperRange + 1;
upperRange = upperRange + 25;
if (range == size - 2)
upperRange++;
}
cout << endl;
return;
}
void holdscrn( ) // void function to hold screen open before exit
{
char holdscreen;
cout << " Enter one character and press return to exit program: ";
cin >> holdscreen;
return;
}
Explanation / Answer
In keeping with 20 lines min per function
Edit & Run
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream> #include <cstdlib> using std::cout; using std::cin; using std::endl; struct Student { Student(); int Quiz; int Assign; int Labs; int Tests; void GetNumEvals(); }; Student::Student() { Quiz = Assign = Labs = Tests = 0; } void Student::GetNumEvals() { std::string myStr; getline(cin, myStr, '-');//ignore the string at the start char symbol; cin >> symbol;//first letter for (int k = 0 ; k < 4; ++k) { if (cin.good()) { symbol = tolower(symbol);//convert everything to lowercase switch(symbol) { case 'q': cin >> Quiz; break; case 'a': cin >> Assign; break; case 'l': cin >> Labs; break; case 't': cin >> Tests; break; default: break; } cin >> symbol; //- cin >> symbol; //letter } } } int main() { Student myStu; myStu.GetNumEvals(); cout <<endl << myStu.Quiz << " Quizzes, " << myStu.Assign << " Assignments, " << myStu.Labs << " Labs, " << myStu.Tests << " Tests" << endl; return 0; } In keeping with 20 lines min per function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <iostream> #include <cstdio> #include <cstdlib> using std::cout; using std::cin; using std::endl; struct Student { void AssignEvals(char); public: Student(); int Quiz; int Assign; int Labs; int Tests; void GetEvals(); }; void Student::AssignEvals(char Letter) { switch(Letter) { case 'q': scanf("%d", &Quiz); break; case 'a': scanf("%d", &Assign); break; case 'l': scanf("%d", &Labs); break; case 't': scanf("%d", &Tests); break; default: break; } } Student::Student() { Quiz = Assign = Labs = Tests = 0; } void Student::GetEvals() { char symbol; do { scanf("%c", &symbol); symbol = tolower(symbol);//convert everything to lowercase AssignEvals(symbol); } while (symbol != 'f'); } int main() { Student myStu; myStu.GetEvals(); cout <<endl << myStu.Quiz << " Quizzes, " << myStu.Assign << " Assignments, " << myStu.Labs << " Labs, " << myStu.Tests << " Tests" << endl; return 0; } Edit & Run
File containing grades looks like class_average -q 5 -a 8 -l 10 -t 2 -f
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.