Algorithm


1. Read the input data (a set of numbers).
2. Calculate the mean (average) of the input data.
3. Calculate the squared difference between each data point and the mean.
4. Sum up all the squared differences.
5. Divide the sum by the total number of data points to get the variance.
6. Take the square root of the variance to get the standard deviation.
7. Print or return the standard deviation as the result.

Code Examples

#1 Code Example- Calculate Standard Deviation using Function

Code - C++ Programming

#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);
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter 10 elements: 1
2
3
4
5
6
7
8
9
10
Standard Deviation = 2.87228
Advertisements

Demonstration


C++ Programing Example to Calculate Standard Deviation-DevsEnv