Signup/Sign In

C++ Program to Calculate Standard Deviation Using Function

In this tutorial, we will learn how to find the standard deviation of any set of numbers using functions in the C++ program.

Standard deviation is the measure of how spread out the numbers in the data are. It is the square root of the variance, where variance is the average of squared differences from the mean.

A program to calculate the standard deviation is given as follows.

Calculate Standard Deviation Using Function

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

float SD(float values[]) // function for calculating standadr deviation
{
    float sum = 0.0, mean, sd = 0.0;

    int i;
    for(i = 0; i < 10; ++i)
    {
        sum = sum + values[i]; // calculating sum
    }
    mean = sum/10; // finding mean.
    for(i = 0; i < 10; ++i)
        sd = sd + pow(values[i] - mean, 2); // calculating standard deviation
    return sqrt(sd / 10);
}
int main()
{
    int i;
    float arr[10];
    cout << "Enter 10 elements: ";
    for(i = 0; i < 10; ++i)
        cin >> arr[i];
    cout << endl << "Standard Deviation = " << SD(arr); // calling function
    return 0;
}


Enter 10 elements: 4 5 7 8 9 6 3 2 1 7

Standard Deviation = 2.5219

Conclusion

Here, we have learned how to find the standard deviation of any set of numbers using functions in the C++ program.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.