A teacher has five students who have taken four tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her four test scores: 90-100 A 80-89 B 70-79 C 60-69 D 0-59 F Write a program that uses an array of string objects to hold the five student names, an array of five characters to hold the five students' letter grades, and five arrays of four doubles to hold each student's set of test scores. The program should allow the user to enter each student's name and his or her four test scores. It should then calculate and display each student's average test score, and a letter grade based on the average. Input Validation: Do not accept test scores less than 0 or great than 100.

Answer :

alimir

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.

Other Questions