Courses

# Program to calculate Standard Deviation

0 Suresh Chand March 30, 2021

In this example, We will calculate the standard deviation of 10 data using array.

To understand this example, You must knowledge in following topics

In this example, we have created the function calculateSD() function and It accepts an array argument. Here, we have passed an array of 10 elements to calculate SD.

## The formula of Standard Deviation

We will use this formula to calculate standard deviation where

## CODE:

#include <iostream>
#include <cmath>
using namespace std;

// function prototype
float calculateSD(float data[]);

//main function start
int main()
{
int i;
float data[10];

//taking 10 numbers from users
cout << "Enter 10 elements: ";
for(i = 0; i < 10; ++i)
cin >> data[i];

//print standard deviation by calling function calculateSD
cout << endl << "Standard Deviation = " << calculateSD(data);

return 0;
}

//main function to calculate standard deviation
float calculateSD(float data[])
{
float sum = 0.0, mean, standardDeviation = 0.0;

int i;

for(i = 0; i < 10; ++i)
{
sum += data[i];
}

mean = sum/10;

for(i = 0; i < 10; ++i)
standardDeviation += pow(data[i] - mean, 2);

return sqrt(standardDeviation / 10);
}

The output of above program is

Enter 10 elements: 1
2
3
4
5
6
7
8
9
10
Subscribe
Notify of
Inline Feedbacks