C++ Program to Calculate Standard Deviation

Here is a C++ program to calculate standard deviation. In this C++ program, we will calculate standard deviation of N numbers stored in an array. Standard Deviation in statistics is a measure that is used to quantify the amount of variation in a set of data. Its symbol is σ (greek letter sigma) is used to represent standard deviation.


C++ Program to Calculate Standard Deviation

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

float findStandardDeviation(float *array, int count);

int main() {
    int count, i;
    float inputArray[500];
    
    cout << "Enter number of elements\n";
    cin >> count;
    
    cout << "Enter " << count <<" elements\n";
    for(i = 0; i < count; i++){
     cin >> inputArray[i];
    }

    cout << "Standard Deviation = " << 
        findStandardDeviation(inputArray, count);

    return 0;
}
// Function to find standard deviation 
float findStandardDeviation(float *array, int count) {
    float sum = 0.0, sDeviation = 0.0, mean;
    int i;

    for(i = 0; i < count; i++) {
        sum += array[i];
    }
    // Calculating mean 
    mean = sum/count;

    for(i = 0; i < count; ++i) {
        sDeviation += pow(array[i] - mean, 2);
    }

    return sqrt(sDeviation/count);
}
Output
Enter number of elements
10
Enter 10 elements
2 4 5 6 8 9 10 13 14 16
Standard Deviation = 4.36005

In above program, we first take N numbers as input from user and store it in an integer array "inputArray". Here we have created a function "findStandardDeviation" calculates and return the value of standard.


Recommended Posts
C++ Program to Find Average of Numbers Using Arrays
C++ Program to Find LCM and GCD of Two Numbers
C++ Program to Calculate Average Percentage Marks
C++ Program to Count Zeros, Positive and Negative Numbers
C++ Program to Make a Simple Calculator
C++ program to Find Factorial of a Number Using Recursion
C++ Program to Check Prime Number Using Function
C++ Program to Convert Decimal Number to Binary Number
C++ Program Linear Search in Array
All C++ Programs