In: Computer Science
c++
Given the test scores of 7 students are 96, 88, 83, 76, 90, 91, 87. Write a program that will calculate the average and the standard deviation.
( Standard deviation is given by sqrt[ sum(score-avg)^2/(N-1) ] )
Code:
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int i;
float sum=0,temp=0;
float scores[] = {96,88,83,76,90,91,87};
// To caluculate sum of scores.
for(i=0;i<7;i++)
{
sum = sum + scores[i];
}
cout<<"The sum of scores is: "<<sum<<endl;
// To calculate average of scores.
float avg = sum/7;
cout<<"average of scores is: "<<avg<<endl;
//STANDARD DEVIATION.
for(i=0;i<7;i++)
temp = temp + pow(scores[i]-avg,2);
float sd = sqrt(temp/7);
cout<<"Standard deviation is: "<<sd<<endl;
return 0;
}
Screenshot:
Output: