std deviation formula code example

Example: standard deviation function in c++

#include<iostream>
#include<cmath>
using namespace std;
int main(int argc, char const *argv[])
{
    double mean;
    double nums[] = {9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4};
    double standardDeviation = 0;
    double arraySize = *(&nums + 1) - nums;

    for (int i = 0; i < arraySize; i++){ mean = (nums[i] + mean);}
    mean = mean/ arraySize;
    for (int i = 0; i < arraySize; i++){
        standardDeviation += pow((nums[i] - mean), 2);
    }
    standardDeviation = sqrt(standardDeviation / arraySize);
    cout << standardDeviation << endl;
    return 0;
}

Tags:

Cpp Example