Please assume that you had the \" scores.dat \" file and show me how the code wi
ID: 670873 • Letter: P
Question
Please assume that you had the " scores.dat " file and show me how the code will look like. Write a program that performs the following tasks: 1. 2. 3. 4. 5. In the main function, declare three float arrays: math, chem, phys. Declare another float array for the total scores: total. You should use a symbolic constant to specify the sizes of the arrays. Declare a variable to store the average of the total scores: ave Total . In the main function, read the scores of students in three courses from a file: "scores.dat". The data file can be downloaded from the class webpage In the file, each row represents a student's record, with each column corresponding to a course Mathematics, Chemistry, and Physics Store the scores in the three float arrays The main function calls a function, computeTotals, to calculate a total score for each student. computeTotals takes the three arrays as parameters and use them to calculate the total scores 6. 7. 8. total = math + chem + phys 9. computeTotals also calculates the average of the total scores of all students 10. computeTotals returns void. In other words, the total scores and the average of total scores must be returned back to the main function implicitly via an array and a reference parameter 11. In the main function, print out the average of total scores: aveTota.l 12. In the main function, save the data to a file, "new_scores.dat", with a four-column format math chem phys total (see the example below). The columns are separated by white spaces 78 85 67 230 94 83 90 267Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
void computeTotals(vector<float> &math,vector<float> &chem,vector<float> &phys,vector<float> &total,float& average){
float sum = 0.0;
for (int i = 0; i < math.size(); i++){
total.push_back(math[i] + chem[i] + phys[i]);
sum += total[i];
}
average = sum/n;
}
int main(){
vector<float> math;
vector<float> chem;
vector<float> phys;
vector<float> total;
infile.open("scores.dat");
while (!infile.eof()){
n++;
stringstream line(s);
int i = 0;
while (line >> temp){ // Split the a string with spaces and store in a vector Token.
if (i == 0){
math.push_back(atof(temp.c_str()));
i++;
}
else if (i == 1){
chem.push_back(atof(temp.c_str()));
i++;
}
else phys.push_back(atof(temp.c_str()));
}
getline(infile,s);
}
infile.close();
float average;
computeTotals(math,chem,phys,total,average);
ofstream outfile;
for (int i = 0; i < n; i++)
outfile.write(math[i]+" "+chem[i]+" "+phys[i]+" "+total[i]+" ");
outfile,write("average marks out of 300 is : "+average+" ");
outfile.close();
return 0;
}