Write a program that will read data from the file \"p6.dat\". The file (that you
ID: 3695588 • Letter: W
Question
Write a program that will read data from the file "p6.dat". The file (that you will create) 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
#include<stdio.h>
#include<iostream>
#include<fstream>
using namespace std;
double calculate(int arr[])
{
double sum = 0,avg;
int i;
for(i=0;i<3;i++)
sum = sum + arr[i];
avg = sum/3;
return avg;
}
int findbest(int s1[],int s2[],int s3[],int s4[],int s5[])
{
int maxi[5],i,max;
maxi[0] = s1[0];
for(i=0;i<3;i++)
if(s1[i]>maxi[0])
maxi[0] = s1[i];
maxi[1] = s2[0];
for(i=0;i<3;i++)
if(s2[i]>maxi[1])
maxi[1] = s2[i];
maxi[2] = s3[0];
for(i=0;i<3;i++)
if(s3[i]>maxi[2])
maxi[2] = s3[i];
maxi[3] = s4[0];
for(i=0;i<3;i++)
if(s4[i]>maxi[3])
maxi[3] = s4[i];
maxi[4] = s5[0];
for(i=0;i<3;i++)
if(s5[i]>maxi[4])
maxi[4] = s5[i];
max=maxi[0];
for(i=0;i<5;i++)
if(max<maxi[i])
max = maxi[i];
return max;
}
int main()
{
string filename,line;
double sum,avg1,avg2,avg3,avg4,avg5,test1,test2,test3;
ifstream infile;
int s1[3],s2[3],s3[3],s4[3],s5[3],count =0,best;
cout << "enter input file name" << endl;
cin >> filename;
infile.open(filename.c_str());
//input data from file to arrays
getline(infile,line);
infile >> s1[count] >> s1[count+1]>> s1[count+2] ;
count = count + 3;
getline(infile,line);
infile >> s2[count] >> s2[count+1]>> s2[count+2] ;
count = count + 3;
getline(infile,line);
infile >> s3[count] >> s3[count+1]>> s3[count+2] ;
count = count + 3;
getline(infile,line);
infile >> s4[count] >> s4[count+1]>> s4[count+2] ;
count = count + 3;
getline(infile,line);
infile >> s5[count] >> s5[count+1]>> s5[count+2] ;
//calculate avg for each student
avg1 = calculate(s1);
avg2 = calculate(s2);
avg3= calculate(s3);
avg4 = calculate(s4);
avg5 = calculate(s5);
cout << "averages per student" << endl;
cout << "first student average" << avg1 << endl;
cout << "second student average" << avg2 << endl;
cout << "third student average" << avg3 << endl;
cout << "fourth student average" << avg4 << endl;
cout << "fifth student average" << avg5 << endl;
cout << "averages per test: " << endl;
sum = s1[0] + s2[0] + s3[0] + s4[0] + s5[0];
test1 = sum/5;
cout << "test1 avg: " << test1 << endl;
sum = s1[1] + s2[1] + s3[1] + s4[1] + s5[1];
test2 = sum/5;
cout << "test2 avg: " << test2 << endl;
sum = s1[3] + s2[3] + s3[3] + s4[3] + s5[3];
test3 = sum/5;
cout << "test1 avg: " << test3 << endl;
best = findbest(s1,s2,s3,s4,s5);
cout << "overall best score: " << best << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.