Write a C++ program that will calculate an average of class test scores and a fr
ID: 3761123 • Letter: W
Question
Write a C++ program that will calculate an average of class test scores and a frequency distribution. Input to the program will consist of integer scores stored in a data file called, DATA602.DAT. Output will consist of the input scores (ten per line), the total number of scores that were input, the total of all scores input, and the class average (two decimal places on this item please). The program will also output the number of scores in each of the following ranges: 90-100 80-89 70-79 60-69 below 60 Your program should also produce an output file named, REPORT602.DAT, which includes all of the above output.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int num, sum = 0, count = 0;
int ninties = 0, eighties = 0, seventies = 0, sixties = 0, others = 0;
ifstream ip;
ip.open("DATA602.DAT");
ofstream op;
op.open("REPORT602.DAT");
while(!ip.eof())
{
ip>>num;
op<<num<<" ";
sum += num;
count++;
if(count%10 == 0)
op<<endl;
if(num >= 90 && num <= 100)
ninties++;
else if(num >= 80 && num < 90)
eighties++;
else if(num >= 70 && num < 80)
seventies++;
else if(num >= 60 && num < 70)
sixties++;
else if(num < 60)
others++;
}
op<<endl;
op<<"The total number of scores in the file are: "<<count<<endl;
op<<"The total of all scores in the file are: "<<sum<<endl;
op<<"The average of the class is: "<<fixed<<setprecision(2)<<(float)sum/count<<endl;
op<<"The number of scores in the range 90 - 100: "<<ninties<<endl;
op<<"The number of scores in the range 80 - 89: "<<eighties<<endl;
op<<"The number of scores in the range 70 - 79: "<<seventies<<endl;
op<<"The number of scores in the range 60 - 69: "<<sixties<<endl;
op<<"The number of scores below 60 : "<<others<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.