// Elaine Torrez #12 CS1A Chapter 11 – Course Grade Problem
/********************************************************************************************
*
* COURSE GRADES
*
* ------------------------------------------------------------------------------------------
* This program keeps a list of students and their test scores. It uses a structure to store
* each student's:
* - name
* - ID number
* - pointer to an array of test scores
* - average test score
* - course grade
*
* The program:
* 1) Asks how many students there are and how many test scores per student.
* 2) Dynamically allocates an array of structures for the students.
* 3) For each student, dynamically allocates an array of test scores.
* 4) Prompts the user to enter name, ID number, and ALL test scores.
* 5) Computes the average test score for each student.
* 6) Assigns a letter grade using this scale:
*
* 91 – 100 : A
* 81 – 90 : B
* 71 – 80 : C
* 61 – 70 : D
* 60 or less: F
*
* 7) Displays a table of each student's name, ID number, average score, and course grade.
* ------------------------------------------------------------------------------------------
*
* INPUT
* numStudents : Number of students in the class
* numTests : Number of tests per student
* For each student:
* name : Student name
* idNum : Student ID number
* tests[] : Each test score (must be >= 0)
*
* OUTPUT
* For each student:
* name
* ID number
* average test score
* course grade
*
* INPUT VALIDATION
* - Number of students and number of tests must be > 0
* - Test scores must not be negative
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
using namespace std;
//------------------------------------------------------------
// Structure to store one student's grade information
//------------------------------------------------------------
struct CourseGrade {
string name; // Student name
int idNum; // Student ID number
float *tests; // Pointer to array of test scores
float average;// Average test score
char grade; // Course grade
};
// Function prototypes
void getStudentData(CourseGrade[], int numStudents, int numTests);
void computeAveragesAndGrades(CourseGrade[], int numStudents, int numTests);
void displayReport(const CourseGrade[], int numStudents);
int main()
{
int numStudents;
int numTests;
cout << "How many students? ";
cin >> numStudents;
while (numStudents <= 0) {
cout << " **ERROR** Enter a value greater than 0: ";
cin >> numStudents;
}
cout << "How many tests per student? ";
cin >> numTests;
while (numTests <= 0) {
cout << " **ERROR** Enter a value greater than 0: ";
cin >> numTests;
}
//--------------------------------------------------------
// Dynamically allocate array of CourseGrade structures
//--------------------------------------------------------
CourseGrade *students = new CourseGrade[numStudents];
//--------------------------------------------------------
// Dynamically allocate test-score arrays for each student
//--------------------------------------------------------
for (int i = 0; i < numStudents; ++i) {
students[i].tests = new float[numTests];
}
//--------------------------------------------------------
// Get all input from the user
//--------------------------------------------------------
getStudentData(students, numStudents, numTests);
//--------------------------------------------------------
// Compute averages and course grades, then display report
//--------------------------------------------------------
computeAveragesAndGrades(students, numStudents, numTests);
displayReport(students, numStudents);
//--------------------------------------------------------
// Free dynamically allocated memory
//--------------------------------------------------------
for (int i = 0; i < numStudents; ++i) {
delete [] students[i].tests;
}
delete [] students;
return 0;
}
//------------------------------------------------------------
// Prompts user for each student's name, ID, and test scores
//------------------------------------------------------------
void getStudentData(CourseGrade s[], int numStudents, int numTests)
{
cout << "\nEnter student information:\n";
cout << "--------------------------\n";
// Clear leftover newline so getline works after using >>
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < numStudents; ++i) {
cout << "Student #" << (i + 1) << " name: ";
getline(cin, s[i].name);
cout << "Student #" << (i + 1) << " ID number: ";
cin >> s[i].idNum;
for (int j = 0; j < numTests; ++j) {
cout << " Test #" << (j + 1) << " score: ";
cin >> s[i].tests[j];
while (s[i].tests[j] < 0.0f) {
cout << " **ERROR** Score cannot be negative. Re-enter: ";
cin >> s[i].tests[j];
}
}
// Clear newline before next getline for name
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl;
}
}
//------------------------------------------------------------
// Computes each student's average and assigns a letter grade
//------------------------------------------------------------
void computeAveragesAndGrades(CourseGrade s[], int numStudents, int numTests)
{
for (int i = 0; i < numStudents; ++i) {
float sum = 0.0f;
for (int j = 0; j < numTests; ++j) {
sum += s[i].tests[j];
}
s[i].average = sum / numTests;
// Assign letter grade based on average
if (s[i].average >= 91.0f)
s[i].grade = 'A';
else if (s[i].average >= 81.0f)
s[i].grade = 'B';
else if (s[i].average >= 71.0f)
s[i].grade = 'C';
else if (s[i].average >= 61.0f)
s[i].grade = 'D';
else
s[i].grade = 'F';
}
}
//------------------------------------------------------------
// Displays a table of student name, ID, average, and grade
//------------------------------------------------------------
void displayReport(const CourseGrade s[], int numStudents)
{
cout << fixed << setprecision(2);
cout << "\nCOURSE GRADE REPORT\n";
cout << "-------------------------------------------------------------\n";
cout << left << setw(20) << "Name"
<< setw(10) << "ID"
<< setw(12) << "Average"
<< "Grade" << endl;
cout << "-------------------------------------------------------------\n";
for (int i = 0; i < numStudents; ++i) {
cout << left << setw(20) << s[i].name
<< setw(10) << s[i].idNum
<< setw(12) << s[i].average
<< s[i].grade << endl;
}
}