Write a program C++ that defines a two-dimension integer array, which stores the
ID: 3902448 • Letter: W
Question
Write a program C++ that defines a two-dimension integer array, which stores the test scores for 10 students and 4 scores each student. The program should have the following functions. Don't use print and scanner. Use << cout and >>cin
getData: this function is to get input of all test scores for each student.
getAverage: This function is to calculate the average score(double type) for each student. No input and output in the function.
getLetterScore: This function is to get the letter score based on their average score. No input and output in the function.
findMaxScore: this function is to find the max average score. No input and output in the function.
Define the necessary variables and arrays in the main function and display the results of each function in the main function. And also make sure which arrays should be passed as const. The function prototypes are shown below (not include const yet):
void getData(int[][cols],int);
void getAverage(int[][cols], int, double[]);
void getLetterScore(double[], int,char[]);
double findMaxScore(double[],int);
average - /4, defined current, double type.
You can not return 10 students.
A=90, B=80, C=70, D=60, F character for score letter (if else statement)
Don’t use constant for max score
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
const int MAX = 50;
// a function called GetData to read and store data into two arrays,
void GetData(ifstream& infile,string name[],int scores[][5],int& n)
{
n = 0;
int i=0;
int j=0;
while(!infile.eof())
{
infile >> name[i];
for(int j=0; j<5; j++)
infile >> scores[i][j];
i++;
}
n = i;
}
char determineGrade(double avg)
{
if(avg>=90 && avg<=100)
return 'A';
else if(avg>=80 && avg<=89)
return 'B';
if(avg>=70 && avg<=79)
return 'C';
if(avg>=60 && avg<=69)
return 'D';
if(avg>=50 && avg<=59)
return 'F';
}
// a function called Average that is used to calculate the average test score and grade,
void Average(int a[][5],char grade[],double avg[],int no_of_students)
{
for(int i=0; i<no_of_students; i++)
{
double sum =0;
for(int j=0; j<5; j++)
sum+= a[i][j];
avg[i] = sum/static_cast<double> (5);
grade[i] = determineGrade(avg[i]);
}
}
// function called PrintResults to output the results.
void PrintResults(string name[],double avg[],int scores[][5],char grade[],int n)
{
for(int i=0; i<n; i++){
cout << left << setw(10)<< name[i];
for(int k=0; k<5; k++)
cout << right << setw(8) << scores[i][k];
cout << endl;
}
cout << setw(8) <<"Average ";
for(int i=0; i<n; i++)
cout << setw(5) << avg[i];
cout << endl;
}
int main()
{
// a one-dimensional array to store the students names,
string name[MAX];
// a (parallel) two-dimensional array to store the test scores,
int scores[MAX][5];
// a parallel one-dimensional array to store grades.
char grade[MAX];
int no_of_students;
double avg[MAX];
ifstream infile("StudentData.txt");
if(!infile)
{
cout <<"unable to open file.so exiting from program" << endl;
return 0;
}
GetData(infile, name, scores, no_of_students);
infile.close();
Average(scores, grade, avg, no_of_students);
PrintResults(name,avg,scores,grade,no_of_students);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.