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

\sigma={\sqrt {\frac {\sum(x_{i}-{\mu})^{2}}{N}}} Standard Deviation
Source : gstatic.com

We will use this formula to calculate standard deviation where

\sigma=population standard deviation
N=the size of the population
x_i=each value from the population
\mu=the population mean

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
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System