C++ for Beginners: C++ Program to Calculate Standard Deviation

(C++ programming Example for Beginners)

C++ Program to Calculate Standard Deviation

This program calculates the standard deviation of 10 data using arrays.

This program calculates the standard deviation of a individual series using arrays. Visit this page to learn about Standard Deviation.

To calculate the standard deviation, calculateSD() function is created. The array containing 10 elements is passed to the function and this function calculates the standard deviation and returns it to the main() function.


Example: Calculate Standard Deviation by Passing it to Function

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

float calculateSD(float data[]);

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

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

    cout << endl << "Standard Deviation = " << calculateSD(data);

    return 0;

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);


Enter 10 elements: 1

Standard Deviation = 2.872281


C++ for Beginners: C++ Program to Calculate Standard Deviation

Sign up to get end-to-end “Learn By Coding” example.

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.