C++ Program to Calculate Standard Deviation

Write a C++ Program to Calculate Standard Deviation with an example. In this C++ standard deviation example, we allow users to enter the total number of items and their prices using for loop. Next, we used another for loop to find out the sum of all the product prices. And the other for loop for variance sum. If you don’t want the user to decide the products and their prices, then you can declare and avoid the for loop.

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
	float Price[50], SD, Mean, Variance, Sum = 0, Varsum = 0;
	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];
	}	
	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;	
	cout << "\nStandard deviation =  " << SD;
		
 	return 0;
}
C++ Program to Calculate Standard Deviation 1

C++ Program to Calculate Standard Deviation using Functions

#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;
}
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