Write a function that computes the average and standard deviation of four scores. The standard deviation is defined to be the square root of the average of the four values: (s i − a) 2 where a is average of the four scores s 1 , s 2 , s 3 , and s 4 . The function will have six parameters and will call two other functions. Embed the function in a driver program that allows you to test the function again and again until you tell the program you are finished.
Program:
#include<iostream>
#include<cmath>
using namespace std;
double calcAverage(double s1, double s2,double s3, double s4);
double calcStandardDeviation(double s1, double s2,double s3, double s4,double average,int n);
void main()
{
double s1,s2,s3,s4;
double avg,sdeviation;
char option;
do
{
cout<<"Enter s1:";
cin>>s1;
cout<<"Enter s2:";
cin>>s2;
cout<<"Enter s3:";
cin>>s3;
cout<<"Enter s4:";
cin>>s4;
avg=calcAverage(s1,s2,s3,s4);
sdeviation=calcStandardDeviation(s1,s2,s3,s4,avg,4);
cout<<"Standard deviation:"<<sdeviation<<endl;
cout<<"Do you want to continue then press y:";
cin>>option;
}while (option='y');
}
double Average(double s1, double s2,double s3, double s4)
{
return (s1+s2+s3+s4)/4;
}
double calcStandardDeviation(double s1, double s2,double s3, double s4, double mean,int n)
{
double sd;
sd=(pow((s1-mean),2)+pow((s2-mean),2)+
pow((s3-mean),2)+pow((s4-mean),2))/n;
sd=sqrt(sd);
return sd;
}
Output:
Enter s1:4
Enter s2:8
Enter s3:16
Enter s4:12
Standard deviation:4.47214
Do you want to continue then press y:n