(process scores in a text file) Suppose that a text file Exercise13_3.txt contai
ID: 3762520 • Letter: #
Question
(process scores in a text file) Suppose that a text file Exercise13_3.txt contains an unspecified number of scores.Write a c++ program that reads the scores from the file and display their total and average.Scores are seperated by blanks.
Instead of displaying the results on the screen, send the results to an output named using your last name.
In addition to finding the average and total, find the number of grades.
Input
Example: Contents of Exercise13_3.txt:
70 88 91 95
66 82 71 99 84 57
80 100 93 73 82
Output
Contents of YourLastName.txt:
Your first and last name
Number of grades = 16
Total of all grades = 1231
Average grade = 82.1
Explanation / Answer
#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;
int main()
{
ifstream infile;
float num;
float total;
float x;
float aver;
x=0; total=0;
infile.open("Exercise13_3.txt");
while(!infile.eof())
{
infile>>num;
total=total+num;
x++;
}
aver=(total-num)/(x-1);
cout << "Number of grades = " <<x<< " ";
cout<< "Total of all grades = " <<total<<' ';
cout<< "Average grade = "<<aver<<' ';
cout<< ""<<' ';
infile.close();
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.