Write a program that takes a list of final scores for a class and determines: 1)
ID: 3569906 • Letter: W
Question
Write a program that takes a list of final scores for a class and determines: 1) the mean (Greek letter mu), 2) the standard deviation (Greek letter sigma), and 3) the number of grades in each category (A, A-, B+, B-, C+, C, C-, D+, D, and F).
Prompt the user for the name of a file containing the final scores, up to 100 scores.
Determine the maximum score, the minimum score, the range of scores (maximum minimum) and a table with column headings:
Grade:
Number of scores:
using the following scoring (where m is the mean, s is the standard deviation, and x is an individual score):
Place the output in a file, in an orderly format, and inform the user of the name of the file before exiting.
Remember to add comments in your program.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int main()
{
ifstream file; // opening file to read
string FileName;
int a[100],n=0;
cout << "Please enter the input filename --> "; // reading file name from user
cin >> FileName;
file.open(FileName.c_str());
while(!file.eof()) // reading from file data to array a;
{
file>>a[n];
n++;
}
int min=a[0],max=a[0],range;
double m,s,sum=0;
for(int i=0;i<n;i++) // finding max, min, range, mean
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
sum=sum+a[i];
}
m=sum/n;
m+=1;
range=max-min;
sum=0;
for(int i=0;i<n-1;i++) // finding the standard deviation
{
sum=sum+((a[i]-m)*(a[i]-m));
}
sum=sum/n+1;
s=sqrt(sum);
double grade[10];
int j=0,count[11]={0};
for(double i=4;i>=-5;i--) // calculating the grade, which is given as the formula
{
grade[j]=m+(i/3)*s;
j++;
}
for(int i=0;i<n;i++) // counting the number of scores of every grade
{
if(grade[0]<=a[i])
count[0]++;
if(grade[1]<=a[i]&&a[i]<grade[0])
count[1]++;
if(grade[2]<=a[i]&&a[i]<grade[1])
count[2]++;
if(grade[3]<=a[i]&&a[i]<grade[2])
count[3]++;
if(grade[4]<=a[i]&&a[i]<grade[3])
count[4]++;
if(grade[5]<=a[i]&&a[i]<grade[4])
count[5]++;
if(grade[6]<=a[i]&&a[i]<grade[5])
count[6]++;
if(grade[7]<=a[i]&&a[i]<grade[6])
count[7]++;
if(grade[8]<=a[i]&&a[i]<grade[7])
count[8]++;
if(grade[9]<=a[i]&&a[i]<grade[8])
count[9]++;
if(grade[10]<=a[i]&&a[i]<grade[9])
count[10]++;
if(a[i]<grade[10])
count[11]++;
}
ofstream out; // opening file for write
out.open("output.txt");
cout<<"Output in the file 'output.txt' ";
out<<" mean: "<<m<<" standard deviation: "<<s;
j=65; //ascii value of j is A
out<<" maximum: "<<max<<" minimum: "<<min<<" range of scores: "<<range;
out<<" Grade "<<"Number of Scores ";
out<<" A "<<count[0];
out<<" A- "<<count[1];
out<<" B+ "<<count[2];
out<<" B "<<count[3];
out<<" B- "<<count[4];
out<<" C+ "<<count[5];
out<<" C "<<count[6];
out<<" C- "<<count[7];
out<<" D+ "<<count[8];
out<<" D "<<count[9];
out<<" F "<<count[10];
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.