CPP Program to Calculate Standard Deviation

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to write a C++ program for calculating the standard deviation of a set of numbers using functions. The program will demonstrate how to calculate the variance of a set of numbers, which is the average of squared differences from the mean, and then its square root will give the standard deviation.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/StandardLibraryGroup(["`Standard Library`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/StandardLibraryGroup -.-> cpp/math("`Math`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") subgraph Lab Skills cpp/comments -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} cpp/variables -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} cpp/data_types -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} cpp/operators -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} cpp/math -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} cpp/for_loop -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} cpp/arrays -.-> lab-96174{{"`CPP Program to Calculate Standard Deviation`"}} end

Define the function to calculate standard deviation

We'll start by defining a function that takes an array of float values and returns the standard deviation of those values.

float SD(float values[]) // function for calculating standard 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);
}

In this function, we first calculate the sum of all the values. We then divide the sum by the total number of values to get the mean. Next, we calculate the variance by summing up the squared differences of each value from the mean. After that, we calculate the standard deviation by taking the square root of the variance.

Write the main function to read input and output results

Next, we write the main function which reads input from the user, stores the values in an array, calls the SD function to get the standard deviation and then displays the result.

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

In this function, we first declare an array arr to store the user's input. We then prompt the user to enter 10 values, one at a time. We then call the SD function with the arr array to get the standard deviation and print it out to the console.

Run the program

Now, we can compile and run the program using the following command in the terminal:

g++ ~/project/main.cpp -o main && ./main

This command will compile the main.cpp file in the ~/project directory and then run the resulting executable.

Verify the output

Let's input 4 5 7 8 9 6 3 2 1 7 as input and check if we are getting the correct output.

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

Standard Deviation = 2.5219

We can see that the standard deviation is 2.5219 which is the same value we obtained when we solved the problem by hand.

Full code

Here is the full code of the main.cpp file for reference:

#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;
}

Summary

In this lab, we learned how to calculate the standard deviation of a set of numbers using functions in a C++ program. We used a function to calculate the variance, which is the average of squared differences from the mean, and then its square root to give the standard deviation. We also learned how to read input from the user, store it in an array, and then call the function to get the standard deviation. Finally, we compiled and ran the program to verify that we were getting the correct output.

Other C++ Tutorials you may like