C++ Program to Find Mean, Variance and Standard Deviation of Array

Write C++ Program to Find Mean, Variance and Standard Deviation of Array

// CPP Program to Find Mean, Variance and Standard Deviation of Array

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int A[] = {21, 23, 41, 62, 32};
	int n = sizeof(A)/sizeof(A[0]), i;

	float sum = 0, Var = 0, Mean, SD;

	cout << "Array A :--> ";
	for(i=0; i<n; i++)
        cout << A[i] << " ";


	for(i = 0;i < n; i++)      //to find the mean
	{
		sum = A[i] + sum;
	}

	Mean = sum/n;
	cout << "\n\nSum :--> " << sum;
	cout << "\nMean :--> " << Mean;


	for(i=0;i<n;i++)	//to find the variance
	{
		Var = Var + ((A[i] - Mean) * (A[i]- Mean)) / (n-1);
	}
	cout << "\nVariance :--> " << Var;


	SD = sqrt(Var);
	cout << "\nStandard Deviation :--> " << SD;
	return 0;
}

Output:

Array A :--> 21  23  41  62  32

Sum :--> 179.00
Mean :--> 35.80
Variance :--> 277.70
Standard Deviation :--> 16.66

≪ Previous | Next ≫