Write a program that will read data from the file \"p6.dat\". The file always co
ID: 3696313 • Letter: W
Question
Write a program that will read data from the file "p6.dat". The file always contains 15 test scores (whole numbers between 0 and 100). The test scores are scores for 5 students taking 3 tests, and are arranged, in the file, by the student - that is the first 3 numbers are the test scores for test 1, 2, and 3 for the first student, etc.
The program will print:
- average per student (5 averages), on a single line, with 2 decimals
- average per test (3 averages), on a single line, with 2 decimals
- overall best score on a single line - how many scores were As (out of the 15, how many were at least 90) on a single line.
To simplify the code, no validations are needed. That is, assume the file is successfully opened, and that all data are 0-100, and that there are exactly 15 numbers in the file. Note that the program reads the filename
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string fileName;
ifstream fin;
int marks[5][3];
cout<<"Enter the name of the file to read the scores: ";
cin>>fileName; //Read the file name.
fin.open(fileName); //Open the file.
for(int i = 0; i < 5; i++) //For each student.
for(int j = 0; j < 3; j++) //Read 3 scores.
fin>>marks[i][j]; //Read the student scores into the array.
//- average per student (5 averages), on a single line, with 2 decimals
cout<<"The average of each student scores is: ";
for(int i = 0; i < 5; i++)
{
double sum = 0;
for(int j = 0; j < 3; j++)
sum += marks[i][j];
cout<<fixed<<setprecision(2)<<sum<<" ";
}
cout<<endl;
//- average per test (3 averages), on a single line, with 2 decimals
cout<<"The average of each test scores is: ";
for(int i = 0; i < 3; i++)
{
double sum = 0;
for(int j = 0; j < 5; j++)
sum += marks[j][i];
cout<<fixed<<setprecision(2)<<sum<<" ";
}
cout<<endl;
int count = 0;
//- overall best score on a single line - how many scores were As (out of the 15, how many were at least 90) on a single line.
for(int i = 0; i < 5; i++)
for(int j = 0; j < 3; j++)
if(marks[i][j] >= 90)
count++;
cout<<"The total number of A's in the student scores are: "<<count<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.