I have a program and put it below. But the weighted average is not working. The
ID: 3662008 • Letter: I
Question
I have a program and put it below. But the weighted average is not working. The formula is 0.3*grade1 + 0.2 *grade2 + 0.2 * grade3 + 0.3 * grade4, but it is not coming correctly. Also the mean and standard deviation is not working properly can anyone help me out?
#include<iostream>
#include<iomanip>
using namespace std;
void computeGrade(int array[][5]);
void computeMean(double score[], int n, double &mean, double &stdeviation);
int main()
{
int grades[10][5]={{1,100,100,100,100},{2,100,100,100,90},{3,100,100,90,90},{4,100,90,90,90},{5,90,90,90,90},{6,90,90,90,80},{7,90,90,80,80},{8,90,80,80,80},{9,80,80,80,80},{10,100,90,80,90}};
computeGrade(grades);
system("pause");
return 0;
}
void computeGrade(int array[][5])
{
double gradetotal=0;
double gradesum=0.0;
double eachAvg[10];
double eachWeightAvg[10];
double gradetotalavg=0.0;
double mean1, mean2,sd1, sd2;
int n=10;
double score[10];
cout<<setw(5) <<"StudentID" << setw(10) << "Grade1" << setw(10) << "Grade2" << setw(10) << "Grade3" << setw(10) << "Grade4" <<
setw(10) <<"Average1" << setw(10) << "Average2" << endl;
cout<<setprecision(2)<<fixed;
for(int i=0;i<10;i++)
{
gradetotal=0;
gradesum=0;
for(int j=0;j<5;j++)
{
cout << setw(10)<< array[i][j] ;
if (j != 0)
{
gradetotal = gradetotal + array[i][j];
if ((j == 1) || (j == 4))
gradesum = gradesum + (0.3 * array[i][j]);
else
gradesum = gradesum + (0.2 * array[i][j]);
}
}
eachAvg[i]=gradetotal/4;
eachWeightAvg[i]=gradesum/4;
cout<<setw(10)<<eachAvg[i]<<setw(10)<<eachWeightAvg[i];
cout<<endl;
}
computeMean(eachAvg,10,mean1,sd1);
computeMean(eachWeightAvg,10, mean2, sd2);
}
void computeMean(double score[], int n, double &mean, double &stdeviation)
{
double m=0;
for(int i=0; i<n; i++)
{
m+=score[i];
}
mean=m/(n*1.0);
cout << "Mean=" << mean << endl;
m=0;
for(int i=0; i<n; i++)
{
m+=pow((score[i]-mean), 2);
}
double average=m/n;
double stdeviation=pow(average, 0.5);
cout<< "Standard Deviation=" << stdeviation << endl;
}
Explanation / Answer
Refer this piece of code :-
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n,i,mean,var,sd,a[100],temp=0,sum=0;
clrscr();
printf("Enter the number of terms:: ");
scanf("%f",&n);
printf("Enter the numbers:: ");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
/*..............Mean.............*/
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
mean=sum/n;
printf("Mean=%f ",mean);
/*.............Variance..........*/
for(i=0;i<n;i++)
{
temp=temp+(a[i]-mean)*(a[i]-mean);
}
var=temp/n;
printf("Variance=%f ",var);
/*............Standard Deviation........*/
sd=sqrt(var);
printf("Standard Deviation=%f ",sd);
getch();
}
output:
This will give you a better understanding to calculate average and standard deviation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.