Answer to Question #187318 in C++ for Alishan khattak

Question #187318

Write a program to calculate students’ average test scores and their grades. You may assume the following input data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three arrays: a one-dimensional array to store the students’ names, a (parallel) two-dimensional array to store the test scores, and a parallel onedimensional array to store grades. Your program must contain at least the following functions: a function to read and store data into two arrays, a function to calculate the average test score and grade, and a function to output the results. Have your program also output the class average.



1
Expert's answer
2021-04-29T19:02:05-0400
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
void readData(vector<string>& names, vector<vector<int>>& grades);
void Average(vector<vector<int>>& grades, vector<int>& average, vector<char>& scr);
char Convert(int x);
void Print(vector<string>& names, vector<vector<int>>& grades, vector<int>& average, vector<char>& scr);
 
 
int main()
{
	vector<string> names;
	vector<vector<int>> grades;
	vector<int> average;
	vector<char> scr;
	readData(names, grades);
	Average(grades, average, scr);
	cout << endl;
	Print(names, grades, average, scr);
	cout << endl;
	return 0;
}
 
void Average(vector<vector<int>>& grades, vector<int>& average, vector<char>& scr)
{
	int sum = 0;
	int avg = 0;
	for (int i = 0; i < grades.size(); i++)
	{
		for (int j = 0; j < 5; j++)
		{
			sum += grades[i][j];
		}
		avg = sum / 5;
		average.push_back(avg);
		scr.push_back(Convert(avg));
		sum = 0;
	}
}
 
void readData(vector<string>& names, vector<vector<int>>& grades)
{
	string str;
	int temp;
	vector<int> values;
	while (true)
	{
		cout << "Enter name (0 - to exit): ";
		cin >> str;
		if (str == "0") break;
		names.push_back(str);
		cout << "Enter score (with space): ";
 
		for (int i = 0; i < 5; i++)
		{
			cin >> temp;
			values.push_back(temp);
		}
		grades.push_back(values);
		values.clear();
	}
}
 
char Convert(int x)
{
	if (x > 90) return 'A';
	if (x > 80) return 'B';
	if (x > 70) return 'C';
	if (x > 60) return 'D';
	return 'F';
}
 
void Print(vector<string>& names, vector<vector<int>>& grades, vector<int>& average, vector<char>& scr)
{
	for (int i = 0; i < names.size(); i++)
	{
		cout << names[i] << '\t';
		for (int j = 0; j < 5; j++)
		{
			cout << grades[i][j] << " ";
		}
		cout << '\t';
		cout << average[i] << '\t' << scr[i] << endl;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS