Having some difficulty writing code for this assignment, though I\'ve seen some
ID: 3720395 • Letter: H
Question
Having some difficulty writing code for this assignment, though I've seen some similar things here on Chegg. When I try to generate the 'p6.dat' file on my PC, the only app that will read it is PSpice. Can I save it from Notepad using 'p6.txt'?
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 O 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 filenameExplanation / Answer
TO CREATE .dat FILE FROM .txt FILE
1 ) CREATE .txt FILE OF REQUIRED DATA AND OPEN IT IN NOTEPAD
2) NOW SELECT "Save As" FROM FILE MENU AND GIVE FILE NAME AS p6.dat AND SELECT FILE TYPE AS "All Files"
3) .DAT FILE IS READY
MY .dat FILE CONTAINS BELOW DATA
98
88
77
56
76
96
64
34
12
45
67
78
90
78
89
NOW REFER BELOW CODE
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
std::ifstream infile; //creating stream for file
int scores[15] = {0}; //array for storing scores
infile.open("p6.dat"); //opening file p6.dat
if(infile.is_open()) //if file is present
{
int i = 0;
while(infile >> scores[i]) //read score line by line
i++;
}
else //if file is not presnt then display message
{
cout<<"Unable to open file"<<endl;
return 0;
}
int student_id = 1;
//finding average of each student which is (test1+test2+test3)/3
for(int i = 0; i < 15; i+=3)
{
cout<<" Average of Student "<<student_id<<" is = "<<std::setprecision(4)<<(scores[i] + scores[i+1] + scores[i+2])/3.0;
student_id++;
}
int test_id = 1;
//finding average of each test
for(int i = 0; i < 3; i++)
{
cout<<" Average of test "<<test_id<<" is "<<std::setprecision(4)<<(scores[i] + scores[i+3] + scores[i+6] + scores[i+9] + scores[i+12])/5.0;
test_id++;
}
//finding best scores and count of scores whose are A's
int best_score = scores[0];
int count_at_least_90 = 0;
for(int i = 0; i < 15; i++)
{
if(best_score < scores[i])
best_score = scores[i];
if(scores[i] >= 90)
count_at_least_90++;
}
cout<<" Overall best score = "<<std::setprecision(4)<<best_score;
cout<<" Count of scores were A's = "<<std::setprecision(4)<<count_at_least_90;
return 0;
}
PLEASE REFER BELOW OUTPUT
Average of Student 1 is 87.6667
Average of Student 2 is 76
Average of Student 3 is 36.6667
Average of Student 4 is 63.3333
Average of Student 5 is 85.6667
Average of test 1 is 70.6
Average of test 2 is 68.6
Average of test 3 is 70.4
Overall best score = 98
Count of scores were A's = 3
Process returned 0 (0x0) execution time : 0.053 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.