// Kurt Feiereisel CSC5 Chapter 7, p.446, #11
/*******************************************************************************
*
* Calculate Student's Grades
* _____________________________________________________________________________
* This program allows a user to enter student's names and test grades for each
* student. The program will then calculate and report the student's average
* of the test grades and a letter grade.
* _____________________________________________________________________________
* Formula:
* avg = total / TESTS
* INPUT:
* names[] : The names of the students
* testscore[] : The scores the student's recieved on their tests
* total : Each student's accumulated test scores
*
* OUTPUT:
* avg : Average for each student's test scores
* letter[] : Each student's letter grade based on their average
* test score
*
*
* ****************************************************************************/
#include <iostream>
#include <string>
using namespace std;
// Initialize Constants
const int STUDS = 5;
const int TESTS = 4;
// Function Prototypes
void students(string names[]);
void testGrades(float testscore[][TESTS], string names[]);
void calcAvg(float averages[], float testscore[][TESTS],string names[]);
void letterGrade(char letter[], float averages[]);
void displayArray(float averages[], char letter[], string names[]);
int main()
{
// Declare Arrays;
string names[STUDS]; // INPUT: student's names
float testscore[STUDS][TESTS]; // INPUT: Student's test scores
float averages[STUDS]; // OUTPUT: Average score for each student
char letter[STUDS]; // OUTPUT: Student's letter grade based on
// test average
// Call Functions
students(names);
testGrades(testscore, names);
calcAvg(averages, testscore, names);
letterGrade(letter, averages);
displayArray(averages, letter, names);
return 0;
}
/*
* Definition of students
* This function accepts input as student's names.
*/
void students(string n[])
{
// Input Student's Names
for (int index = 0; index < STUDS; index++)
{
cout << "Please enter students names:" << endl;
cin >> n[index];
}
}
/*
* Definition of testGrades
* This function accepts input as Test scores for each test for each
* student.
*/
void testGrades(float t[][TESTS], string n[])
{
// Input Test Scores
for (int index = 0; index < STUDS; index++)
{
cout << "Enter " << n[index] << " test scores: \n";
for (int testIndex = 0; testIndex < TESTS; testIndex++)
{
cout << "Test " << (testIndex + 1) << ": ";
cin >> t[index][testIndex];
// Input Verification
while( (t[index][testIndex] < 0) || t[index][testIndex] > 100)
{
cout << "Please enter a grade between 0 and 100." << endl;
cin >> t[index][testIndex];
}
}
cout << endl;
}
}
/*
* Definition of calcAvg
* This function will calculate the averages foe each student
*/
void calcAvg(float a[], float t[][TESTS], string n[])
{
// Initialize Variables
float total;
float avg;
// Accumulate total score for each student
for(int index = 0; index < STUDS; index++)
{
total = 0;
for(int count = 0; count < TESTS; count++)
{
total += t[index][count];
}
// Calcluate Average for each student
avg = total / TESTS;
// Store average for student in average array
a[index] = avg;
}
}
/*
* Definition of letterGrade
* This function will take the averages of each student and convert it to a
* letter grade.
*/
void letterGrade(char l[], float a[])
{
// Determine Letter Grade based on Average
for(int index = 0; index < STUDS; index++)
{
if(a[index] <= 100 && a[index] >= 90)
l[index] = 'A';
else if(a[index] < 90 && a[index] >= 80)
l[index] = 'B';
else if(a[index] < 80 && a[index] >= 70)
l[index] = 'C';
else if(a[index] < 70 && a[index] >= 60)
l[index] = 'D';
else if(a[index] < 60 && a[index] >= 0)
l[index] = 'F';
else
cout << "Invalid" << endl;
}
}
/*
* Definition of displayArray
* This function will display the letter grade and average grade of each student
*/
void displayArray(float a[], char l[], string n[])
{
// Display Student's names, averages, and letter grades
for(int index = 0; index < STUDS; index++)
{
cout << n[index] << "'s" << " results:" << endl;
cout << "\tScore: " << a[index] << endl;
cout << "\tGrade: " << l[index] << endl;
}
}