- Related Questions & Answers
- C++ program to implement standard deviation of grouped data
- PHP program to find standard deviation of values within an array
- Program for Mean Absolute Deviation in C++
- C program to calculate age
- C++ Program to calculate Double Integration
- Absolute Deviation and Absolute Mean Deviation using NumPy
- C++ Program to implement standard error of mean
- C++ Program to Calculate Power Using Recursion
- C++ program to calculate Profit Or Loss
- C++ Program to Calculate Sum of Natural Numbers
- C++ Program to Calculate Power of a Number
- C program to calculate the value of nPr?
- Program to calculate volume of Ellipsoid in C++
- Program to calculate value of nCr in C++
- C++ Program to calculate Bitonicity of an Array

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Standard deviation is the measure of how spread out the numbers in the data are. It is the square root of variance, where variance is the average of squared differences from the mean.

A program to calculate the standard deviation is given as follows.

#include <iostream> #include <cmath> using namespace std; int main() { float val[5] = {12.5, 7.0, 10.0, 7.8, 15.5}; float sum = 0.0, mean, variance = 0.0, stdDeviation; int i; for(i = 0; i < 5; ++i) sum += val[i]; mean = sum/5; for(i = 0; i < 5; ++i) variance += pow(val[i] - mean, 2); variance=variance/5; stdDeviation = sqrt(variance); cout<<"The data values are: "; for(i = 0; i < 5; ++i) cout<< val[i] <<" "; cout<<endl; cout<<"The standard deviation of these data values is "<<stdDeviation; }

The data values are: 12.5 7 10 7.8 15.5 The standard deviation of these data values is 3.1232

In the above program, first the sum of the data values is obtained. Then the mean is found by dividing the sum by the number of elements. This is given in the following code snippet.

for(i = 0; i < 5; ++i) sum += val[i]; mean = sum/5;

The variance of the data is found by squaring the differences from the mean, adding them and then finding their average. This is shown in the following code snippet.

for(i = 0; i < 5; ++i) variance += pow(val[i] - mean, 2); variance=variance/5;

The standard deviation is found by obtaining the square root of the variance. Then all the data values and the standard deviation is displayed. This is given as follows.

stdDeviation = sqrt(variance); cout<<"The data values are: "; for(i = 0; i < 5; ++i) cout<< val[i] <<" "; cout<<endl; cout<<"The standard deviation of these data values is "<<stdDeviation;

Advertisements