// Example program
#include <iostream>
#include <vector>
#include <cmath>
using Data = double;
Data variance(const std::vector<Data>& vec)
{
Data var = 0;
// hint: you'll need a for loop here
return var;
}
Data std_dev(const std::vector<Data>& vec)
{
return std::sqrt(variance(vec));
}
int main()
{
std::vector<Data> vec(10);
// fill in vector with whatever data
for (size_t i = 0; i < vec.size(); i++)
{
vec[i] = (2017 * i) % vec.size();
}
// call variance function:
Data var = variance(vec);
// call standard deviation function:
Data standard_deviation = std_dev(vec);
}
Also be aware of the differences in variance/stdev definitions, depending on what your data is (you most likely don't have to worry about this for your purposes, though): https://en.wikipedia.org/wiki/Bessel%27s_correction
Variance here is that of your given data. If you are using your data to estimate both variance AND mean of a larger population of which your sample is only a small part, multiply the variance by N/(N-1) to get an unbiased estimator. The mean is already unbiased.