// Torrez, Elaine CS1A
// Chapter 7, P. 447, #11
// *****************************************************************************************
// * GRADE BOOK *
// *---------------------------------------------------------------------------------------*
// * This program stores student names and their test scores using parallel arrays. *
// * For each student, it validates five test scores (0–100), computes the average, *
// * assigns a letter grade, and prints a formatted report. *
// * *
// * INPUT: *
// * names[] : Student names *
// * scores[][TESTS] : Five test scores per student (0–100) *
// * *
// * OUTPUT: *
// * avg[] : Average score per student *
// * letter[] : Letter grade per student (A/B/C/D/F) *
// *****************************************************************************************
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
/***** CONSTANTS *****/
const int STUDENTS = 5; // number of students
const int TESTS = 5; // tests per student
/***** PARALLEL ARRAYS *****/
string names[STUDENTS];
double scores[STUDENTS][TESTS];
double avg[STUDENTS];
char letter[STUDENTS];
/***** INPUT SECTION *****/
cout << "Enter data for " << STUDENTS << " students.\n";
cout << "(Each student has " << TESTS << " test scores between 0 and 100.)\n\n";
for (int s = 0; s < STUDENTS; s++)
{
cout << "Student " << (s + 1) << " name: ";
getline(cin, names[s]);
if (names[s].empty()) { // if leftover newline or empty, prompt again
getline(cin, names[s]);
}
for (int t = 0; t < TESTS; t++)
{
cout << " Test " << (t + 1) << " score: ";
cin >> scores[s][t];
// Validate: 0–100
while (scores[s][t] < 0.0 || scores[s][t] > 100.0)
{
cout << " Invalid. Enter a score between 0 and 100: ";
cin >> scores[s][t];
}
}
cin.ignore(10000, '\n'); // clear newline before next getline
cout << "\n";
}
/***** PROCESSING SECTION *****/
for (int s = 0; s < STUDENTS; s++)
{
double sum = 0.0;
for (int t = 0; t < TESTS; t++)
sum += scores[s][t];
avg[s] = sum / TESTS;
if (avg[s] >= 90.0) letter[s] = 'A';
else if (avg[s] >= 80.0) letter[s] = 'B';
else if (avg[s] >= 70.0) letter[s] = 'C';
else if (avg[s] >= 60.0) letter[s] = 'D';
else letter[s] = 'F';
}
/***** OUTPUT SECTION *****/
cout << fixed << setprecision(2);
cout << "====================== GRADE BOOK REPORT ======================\n";
cout << left << setw(20) << "Name"
<< right << setw(8) << "Avg"
<< setw(8) << "Grade" << "\n";
cout << "---------------------------------------------------------------\n";
for (int s = 0; s < STUDENTS; s++)
{
cout << left << setw(20) << names[s]
<< right << setw(8) << avg[s]
<< setw(8) << letter[s] << "\n";
}
cout << "===============================================================\n";
return 0;
}