Write a program that will a) Prompt the user to enter numerical grades of studen
ID: 3627123 • Letter: W
Question
Write a program that willa) Prompt the user to enter numerical grades of students in a test, one at a time, and store it in an array. This is accomplished in the main module. The user enters a negative number to indicate all the grades are entered. The program will keep the count of the number of grades entered.
b) Display all the grades in a tabular form.
c) Call function to calculate the average grade of the class, the median, and the lowest and highest grades.
d) Finally display the class average, highest grade, the lowest grade, and the median at the bottom. At the end the user is prompted to exit or enter grades for another class.
Procedure or Details:
The array is passed to the function.
All calculations are done in the function.
The values are displayed in the main module.
The following is an example of the output:
Number Grade
---------- --------
1 65
2 90
3 72
4 86
5 89
6 62
7 82
8 92
9 95
10 68
The class average: 80.1
Highest Grade is: 92
Lowest Grade: 62
Median Grade: 84
This requires sorting of the array. The median is the middle number of the sorted grades if the number of grades is odd. The median is average of the two middle numbers if the number of grades is even. In the above example, the median is (82+86)/2 = 84
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
void calculate(int[],int,double&,double&,int&,int&);
int main()
{int a[50],i=0,j,low,high,again;
double average,median;
do
{i=0;
cout<<"Enter a grade (<0 to exit): ";
cin>>a[i];
while(a[i]>=0)
{i++;
cout<<"Enter a grade (<0 to exit): ";
cin>>a[i];
}
cout<<"Number Grade ------ ----- ";
for(j=0;j<i;j++)
cout<<j+1<<" "<<a[j]<<endl;
calculate(a,i,average,median,low,high);
cout<<"The class average: "<<average<<endl;
cout<<"Highest Grade is: "<<high<<endl;
cout<<"Lowest Grade: "<<low<<endl;
cout<<"Median Grade: "<<median<<endl;
cout<<"again?(1 for yes, 2 for no): ";
cin>>again;
}while(again==1);
return 0;
}
void calculate(int a[],int n,double& avg,double& median,int& high,int& low)
{int i,j,t,sum=0;;
for(i=0;i<n-1;i++)
{for(j=i;j<n;j++)
if(a[i]<a[j])
{t=a[i];
a[i]=a[j];
a[j]=t;
}
}
for(i=0;i<n;i++)
sum+=a[i];
avg=sum/(double)n;
low=a[0];
high=a[n-1];
if(n%2==0)
median=(a[n/2]+a[n/2-1])/2.;
else
median=a[n/2];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.