Develop a program which reads data from an in sequential access file (scores dat
ID: 3805025 • Letter: D
Question
Develop a program which reads data from an in sequential access file (scores dat), manipulates the put data and writes the output data into another sequent access file (scores.out). The input file scores.dat is provided to you beforehand. It contains the students' id numbers, scores of test-1 test-2 and test-3 in each row. You will read data from scores.dat file and calculate each student's lowest score, highest score and average score. Then you will write students id number, student's lowest score, highest score and average score to the output file scores.out scores.out file has to have an appropriate header for each column. Assume that you do not know how many records are there in the input file. Followings are the sample snapshots of input file and, the output file after you run your program:Explanation / Answer
scores.txt (Save the file under D Drive .Then the path of the file pointing to it is D:\scores.txt )
1111 56 88 82
2222 95 93 95
3333 67 91 66
4444 89 69 69
5555 78 80 90
6666 99 96 100
7777 79 86 84
8888 100 98 99
9999 45 85 75
___________________
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Declaring variables
int id,test1,test2,test3,min,max;
double average=0.0,sum;
int scores[3];
//Opening the input file
dataIn.open("D:\scores.dat");
//creating and Opening the output file
dataOut.open("D:\scores.out");
//Setting the precision of two decimal places
dataOut<<setprecision(2)<<fixed<<showpoint;
dataOut<<"ID Lowest Highest Average ";
dataOut<<"== ====== ======= ======= ";
/* This while loop will read the data from
* the file and find minimum,maximum,average
* and written the output into another file
*/
while(dataIn>>id>>test1>>test2>>test3)
{
sum=0.0;
scores[0]=test1;
scores[1]=test2;
scores[2]=test3;
min=scores[0];
max=scores[0];
/* This for loop will find the
* minimum and maximum test scores
*/
for(int i=0;i<3;i++)
{
if(min>scores[i])
min=scores[i];
if(max<scores[i])
max=scores[i];
//calculating the sum of test scores of each student
sum+=scores[i];
}
//calculating the average
average=sum/3;
//Writing the data to the output file
dataOut<<id<<" "<<min<<" "<<max<<" "<<average<<" ";
}
cout<<"** Written data to the File **"<<endl;
//Closing the intput file
dataIn.close();
//Closing the output file.
dataOut.close();
return 0;
}
_______________________
scores.out (We will find this file under D Drive.As we specified the output file path as D:\scores.out )
ID Lowest Highest Average
== ====== ======= =======
1111 56 88 75.33
2222 93 95 94.33
3333 66 91 74.67
4444 69 89 75.67
5555 78 90 82.67
6666 96 100 98.33
7777 79 86 83.00
8888 98 100 99.00
9999 45 85 68.33
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.