How to Calculate Standard Deviation in C++

Suraj P Feb 12, 2024
  1. Calculate Standard Deviation in C++ Using a Raw Loop
  2. Calculate Standard Deviation in C++ Using Standard Template Library (STL)
  3. Conclusion
How to Calculate Standard Deviation in C++

Understanding the statistical properties of a dataset is a significant aspect of data analysis, providing valuable insights into its variability. Among the key metrics used for this purpose is the standard deviation, a measure that quantifies the spread of data points relative to the mean.

In C++ programming, knowing how to calculate standard deviation is essential for developers engaged in data-centric applications, scientific computing, and financial modeling. This article explores various methods, from traditional raw loops to leveraging the Standard Template Library (STL), offering you a comprehensive guide to effectively compute standard deviation within your code.

Calculate Standard Deviation in C++ Using a Raw Loop

Calculating the standard deviation in C++ involves several steps. In this section, we’ll use a raw loop to perform the calculations.

Standard deviation is a statistical measure that indicates how spread out the numbers in a dataset are relative to the mean.

Steps to Calculate Standard Deviation Using a Raw Loop

  • Iterate through the dataset and calculate the mean by summing up all the elements and dividing by the total number of elements.
  • Iterate through the dataset again. For each element, subtract the mean and square the result.
  • Find the average of the squared differences obtained in step 2. This represents the variance.
  • Take the square root of the variance obtained in step 3 to get the standard deviation.

Now, let’s provide a complete working example using C++ with a raw loop:

#include <cmath>
#include <iostream>

double calculateMean(int arr[], int size) {
  double sum = 0;
  for (int i = 0; i < size; ++i) {
    sum += arr[i];
  }
  return sum / size;
}

double calculateStdDev(int arr[], int size) {
  double mean = calculateMean(arr, size);
  double sumSquaredDiff = 0;

  for (int i = 0; i < size; ++i) {
    sumSquaredDiff += pow(arr[i] - mean, 2);
  }

  return sqrt(sumSquaredDiff / size);
}

int main() {
  // Example dataset
  int data[] = {100, 200, 300, 400, 500};
  int size = sizeof(data) / sizeof(data[0]);

  // Calculate standard deviation
  double stdDev = calculateStdDev(data, size);

  // Output the result
  std::cout << "Mean: " << calculateMean(data, size) << std::endl;
  std::cout << "Variance: " << stdDev * stdDev << std::endl;
  std::cout << "Standard Deviation: " << stdDev << std::endl;

  return 0;
}

In this C++ program, we define two functions, calculateMean and calculateStdDev.

The calculateMean function takes an integer array arr and its size size as parameters. It initializes a variable sum to zero and uses a for loop to iterate through each element of the array, accumulating their sum in the variable sum.

It returns the mean by dividing the sum by the size.

On the other hand, the calculateStdDev function calculates the standard deviation. It takes the array arr and its size size as parameters.

It first calls calculateMean to obtain the mean of the dataset. Then, it initializes a variable sumSquaredDiff to zero.

Using another for loop, it iterates through the array, subtracts the mean from each element, squares the result using pow, and adds up these squared differences in sumSquaredDiff. The function returns the standard deviation by taking the square root of the average of the squared differences.

Moving on to the main function, an example dataset data is defined, consisting of the values {100, 200, 300, 400, 500}. The variable size is calculated as the total number of elements in the array divided by the size of a single element.

The program then calculates the standard deviation by calling the calculateStdDev function with the dataset and its size as arguments. Finally, it outputs the mean, variance, and standard deviation using std::cout.

Code Output:

Calculate Standard Deviation Using a Raw Loop

This C++ program calculates the standard deviation using a raw loop, making it easy to understand and implement for datasets of various sizes.

Calculate Standard Deviation in C++ Using Standard Template Library (STL)

We can also calculate the standard deviation in C++ using the Standard Template Library (STL). Utilizing the STL can streamline the code and make it more concise.

Steps to Calculate Standard Deviation Using the STL

  • Instead of raw arrays, use a vector from the STL to store the dataset. Vectors are dynamic arrays in C++ and provide flexibility in handling variable-sized datasets.
  • Utilize the std::accumulate algorithm to sum up the elements of the vector. Divide the sum by the vector’s size to obtain the mean.
  • Use a lambda function to calculate the squared differences between each element and the mean. This eliminates the need for an explicit loop.
  • Again, employ std::accumulate to find the sum of squared differences. Divide the sum by the vector’s size to get the variance. Take the square root of the variance to obtain the standard deviation.

Now, let’s provide a complete working example using C++ with the STL:

#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>

double calculateStdDev(const std::vector<int>& data) {
  double mean = std::accumulate(data.begin(), data.end(), 0.0) / data.size();

  double sumSquaredDiff = std::accumulate(
      data.begin(), data.end(), 0.0,
      [mean](double acc, int value) { return acc + pow(value - mean, 2); });

  return sqrt(sumSquaredDiff / data.size());
}

int main() {
  // Example dataset
  std::vector<int> data = {100, 200, 300, 400, 500};

  // Calculate standard deviation
  double stdDev = calculateStdDev(data);

  // Output the result
  std::cout << "Mean: "
            << std::accumulate(data.begin(), data.end(), 0.0) / data.size()
            << std::endl;
  std::cout << "Variance: " << stdDev * stdDev << std::endl;
  std::cout << "Standard Deviation: " << stdDev << std::endl;

  return 0;
}

Here, we have a calculateStdDev function that takes a vector data as its parameter. It uses std::accumulate to calculate the mean of the dataset by summing up its elements and dividing the result by the vector’s size.

Next, it utilizes another std::accumulate with a lambda function to calculate the sum of squared differences between each element and the mean. The lambda function takes two parameters: the accumulator (acc) and the current value (value).

It returns the accumulated sum plus the squared difference between the current value and the mean. Finally, the function returns the standard deviation by taking the square root of the average of squared differences.

In the main function, an example dataset data is defined using a vector containing values similar to the previous example. The program then calculates the standard deviation by calling the calculateStdDev function with the dataset as an argument.

Code Output:

Calculate Standard Deviation Using the STL

This C++ program leverages the Standard Template Library to calculate the standard deviation, making the code more concise and expressive. Using vectors and STL algorithms enhances readability and ease of use, particularly for datasets with varying sizes.

Conclusion

In conclusion, calculating standard deviation in C++ involves several approaches, each catering to specific preferences and dataset structures. Whether utilizing raw loops or the Standard Template Library (STL) for a more concise implementation, the goal remains the same – to measure the dispersion of data points from the mean.

The choice between methods depends on factors such as code readability, efficiency, and the dynamic nature of the dataset. By understanding and implementing these methods, C++ programmers can confidently analyze and interpret the variability within their datasets, gaining valuable insights into statistical properties.

Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - C++ Math