//Maxwell Brewer CS1A Chapter 11, p. 648, #12
//
/*******************************************************************************
* STUDENT GRADE CALCULATOR
* _____________________________________________________________________________
* This program collects information about students, including their names,
* ID numbers, and test scores. It calculates the average test score and assigns
* a letter grade based on the average.
*
* INPUT:
* - Number of students and number of tests per student.
* - Each student’s name, ID number, and test scores.
*
* OUTPUT:
* - A summary table displaying each student’s name, ID, average score,
* and corresponding letter grade.
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
// Structure to store student information
struct Student {
string name;
int ID;
int* testScores;
double average;
char grade;
};
// Function prototypes
double calculateAverage(const Student &student, int numTests);
char determineLetterGrade(double average);
int main() {
// Variables to hold the number of students and tests
int numStudents, numTests;
// Input number of students and tests
cout << "Enter the number of students: ";
cin >> numStudents;
cout << "Enter the number of tests per student: ";
cin >> numTests;
// Dynamic allocation of student array
Student* students = new Student[numStudents];
// Input data for each student
cout << "\nEnter data for each student:\n";
for (int i = 0; i < numStudents; i++) {
cout << "\nStudent " << (i + 1) << ":\n";
// Input student name
cout << "Name: ";
cin.ignore(); // Clear input buffer
getline(cin, students[i].name);
// Validate name input
while (students[i].name.empty()) {
cout << "Name cannot be empty. Please enter the name: ";
getline(cin, students[i].name);
}
// Input student ID
cout << "ID Number: ";
cin >> students[i].ID;
// Allocate memory for test scores
students[i].testScores = new int[numTests];
// Input test scores
for (int j = 0; j < numTests; j++) {
cout << "Test " << (j + 1) << ": ";
cin >> students[i].testScores[j];
// Validate nonnegative test scores
while (students[i].testScores[j] < 0) {
cout << "Test scores must be nonnegative. Enter again: ";
cin >> students[i].testScores[j];
}
}
// Calculate average and grade
students[i].average = calculateAverage(students[i], numTests);
students[i].grade = determineLetterGrade(students[i].average);
}
// Display results
cout << "\n\nStudent Grade Report:\n";
cout << "-------------------------------------------\n";
cout << "Name\t\tID\t\tAverage\t\tGrade\n";
cout << "-------------------------------------------\n";
for (int i = 0; i < numStudents; i++) {
cout << students[i].name << "\t\t"
<< students[i].ID << "\t\t"
<< students[i].average << "\t\t"
<< students[i].grade << "\n";
}
// Free allocated memory
for (int i = 0; i < numStudents; i++) {
delete[] students[i].testScores;
}
delete[] students;
return 0;
}
/**
* Calculates the average test score for a student.
*
* @param student The student whose average is to be calculated.
* @param numTests The number of tests the student has taken.
* @return The calculated average score as a double.
*/
double calculateAverage(const Student &student, int numTests) {
double total = 0;
// Sum all test scores
for (int i = 0; i < numTests; i++) {
total += student.testScores[i];
}
return total / numTests; // Calculate and return average
}
/**
* Determines the letter grade based on the average score.
*
* @param average The average score.
* @return The corresponding letter grade.
*/
char determineLetterGrade(double average) {
if (average >= 91) {
return 'A';
} else if (average >= 81) {
return 'B';
} else if (average >= 71) {
return 'C';
} else if (average >= 61) {
return 'D';
} else {
return 'F';
}
}