Answer:
#include <iostream>
using namespace std;
int main()
{
const int numofStudents = 5;//number of students
const int numofTests = 2;//number of tests taken by students
const int grade = 5;//number of grades available
char letter[grade] = { 'A','B','C','D','F' };
double table[numofStudents][numofTests];//defining 2darray with names and scores
string names[numofStudents];//storing names of students
for (int num = 0; num < numofStudents; num++)//storing names and scores of students
{
cout << "Enter student's name: ";
cin >> names[num];
for (int test = 0; test < numofTests; test++)//scores loop
{
cout << "Enter the test score: ";
cin >> table[num][test];
if (table[num][test] < 0 || table[num][test]>100)//input validation
{
cout << "Invalid input. Try again" << endl;
cin >> table[num][test];//input again
}
}
}
for (int i = 0;i < numofStudents;i++)//calculating averages and grades
{
int sum = 0;
cout << "Test scores for " << names[i] << ": " << endl;
for (int j = 0;j < numofTests;j++)
{
cout << table[i][j] << " ";
sum += table[i][j];//summing scores
}
cout << endl;
float average = (float)sum / numofTests;//calculating the average
cout << "Making an average of: "<< average;
if (average <= 100 && average >= 90)//assigning a grade
{
cout << " and has received an " << letter[0];
}
else if (average <= 89 && average >= 80)
{
cout << " and has received a " << letter[1];
}
else if (average <= 79 && average >= 70)
{
cout << " and has received a " << letter[2];
}
else if (average <= 69 && average >= 60)
{
cout << " and has received a " << letter[3];
}
else if (average <= 59 && average >= 0)
{
cout << " and has received an " << letter[4];
}
else
{
cout << "Invalid scores entered." << endl;
}
cout << endl;
}
return 0;
}
Explanation:
We first define the number of students, number of tests and number of grades available for assignments. We then fill in the 2D array of students names and their scores making sure assigning more than 100 and less than 0 is not allowed. We then sum the scores and retrieve an average. Using the average, a letter grade is assigned to that individual.