C++ Program to Calculate Standard Deviation Using Function

#include<iostream>
#include<math.h>
using namespace std;
float StandardDeviation (float Price[], int Number)
{
	float Mean, Variance, SD, Sum = 0, Varsum = 0;
  	int i;
  	for(i = 0; i<Number; i++)
	{
		Sum = Sum + Price[i];
	}
	Mean = Sum /(float)Number;
	for(i=0; i<Number; i++)
	{
		Varsum = Varsum + pow((Price[i] - Mean),2);
	}
	Variance = Varsum / (float)Number;
	SD = sqrt(Variance);
	cout << "\nMean                = " << Mean;
	cout << "\nVarience            = " << Variance;
	return SD;
}
int main()
{
	float Price[50], SD;
   int  i, Number;
	cout << "Please Enter the N Value  =  ";
	cin >> Number;
	cout << "Please Enter the Real Numbers upto " << Number <<"\n";
	for(i = 0; i < Number; i++)
	{
		cin >> Price[i];
	}
	SD = StandardDeviation (Price, Number);
	cout << "\n\nStandard deviation =  " << SD;
 	return 0;
}

OUTPUT:

Please Enter the N Value  =  5
Please Enter the Real Numbers upto 5
11.55 22.90 33.70 70 60.87
Mean                = 39.804
Varience            = 495.373
Standard deviation =  22.257

Leave a Comment