Standard deviation c++ – C++ Program to Calculate Standard Deviation

Standard deviation c++: In the previous article, we have discussed C++ Program to Find Quotient and Remainder. In this article, we will see C++ Program to Calculate Standard Deviation.

C++ Program to Calculate Standard Deviation

  • Write 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

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.

If you are a newbie to the programming languages and want to explore all Example C++ Program for acquiring the best coding skills. Have a look at our C++ Programs Tutorial.