C++ program to calculate Mean,Variance& Standard Deviation by Short-Cut method

/* Program in C++ to calculate mean,variance &
standard deviation using short-cut method.
Inputting the no. of terms, C.I. & fi.
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main( )
{
clrscr( );
int s_no,i,j;
long double ci[20][20],sum=0,xi[20],h,fi[20],N=0,A,yi[20],fiyi[20],sum1=0,yi2[20],fiyi2[20],sum2=0,var; //Symbols have their usual meaning.
char choice;
for (;;) //Loop to continue.
{
start:
clrscr( );
cout<<"Enter how many terms (max. 20) :\t";
cin>>s_no; //Inputting the no. of terms.
clrscr( );
cout<<"Enter the C.I. :\n";
for (i=0;i<s_no;i++)
{
sum=0;
for (j=0;j<2;j++)
{
cin>>ci[i][j]; //Inputting C.I.
if (j==0)
cout<<"-";
sum+=ci[i][j]; //Sum of class interval to find xi.
}
xi[i]=sum/2; //Calculating xi.
cout<<"\n";
}
h=xi[1]-xi[0]; //Calculating difference between xi.
clrscr( );
cout<<"Enter the frequency :\n";
for (i=0;i<s_no;i++)
{
cin>>fi[i]; //Inputting fi.
N+=fi[i]; //Sum of fi.
if (s_no%2==0)
A=xi[(s_no/2)-1]; //If there are even no. of terms. //Calculating assumed mean.
else
A=xi[(s_no-1)/2]; //If there are odd no. of terms.
}
for (i=0;i<s_no;i++)
{
yi[i]=(xi[i]-A)/h; //Calculating yi.
fiyi[i]=fi[i]*yi[i]; //Calculating fiyi.
sum1+=fiyi[i]; //Sum of fiyi.
yi2[i]=pow(yi[i],2); //Calculating yi^2.
fiyi2[i]=fi[i]*yi2[i]; //Calculating fiyi^2.
sum2+=fiyi2[i]; //Sum of fiyi^2.
}
clrscr( );
cout<<"C.I.\t\t xi\t fi\t yi\t yi^2\t fiyi\t fiyi^2 \n\n";
for (i=0;i<s_no;i++)
{
for (j=0;j<2;j++)
{
if (j==0)
cout<<ci[i][j]<<"-"; //Printing Class interval.
else
cout<<ci[i][j];
}
cout<<"\t\t"<<xi[i]<<"\t"<<fi[i]<<"\t"<<yi[i]<<"\t"<<yi2[i]<<"\t"<<fiyi[i]<<"\t"<<fiyi2[i]<<"\n"; //Printing table.
}
cout<<"\nSum:\t\t\tN= "<<N<<"\t\t\t"<<sum1<<"\t"<<sum2; //Printing sum of fi,fiyi & fiyi^2.
cout<<"\n\nMean \t\t\t:\t"<<A+((sum1/N)*h); //Printing & calculating Mean.
var=(((h*h)/(N*N))*(N*sum2-(sum1*sum1))); //Calculating Variance.
cout<<"\nVariance \t\t:\t"<<var; //Printing Variance.
cout<<"\nStandard Deviation \t:\t"<<sqrt(var); //Printing & calculating Standard Deviation.
cout<<"\n\nPress 1 to CONTINUE or any key to EXIT\n";
cin>>choice;
if (choice=='1') //Checking if user presses 1 then continue.
goto start;
else
break;
} //If user presses any other key then exit.
getch( );
}



OUTPUT==>

Enter how many terms (max. 20) : 5

Enter the C.I. :
0
-10

10
-20

20
-30

30
-40

40
-50

Enter the frequency :
5
8
15
16
6

C.I. xi fi yi yi^2 fiyi fiyi^2
0-10 5 5 -2 4 -10 20
10-20 15 8 -1 1 -8 8
20-30 25 15 0 0 0 0
30-40 35 16 1 1 16 16
40-50 45 6 2 4 12 24

Sum: N=50 10 68

Mean : 27
Variance : 132
Standard Deviation : 11.489125

Press 1 to CONTINUE or any key to EXIT
Last edited on
Topic archived. No new replies allowed.