Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C++ Program to Calculate Standard Deviation
This program calculates the standard deviation of 10 data using arrays.
To understand this example, you should have the knowledge of the following C++ programming topics:
This program calculates the standard deviation of an 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 using 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);
}
Output
Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 Standard Deviation = 2.87228
Also Read:
Did you find this article helpful?