When I call these two functions to calculate the mean and standard deviation, my standard deviation is off by a little bit. I think it has to do with my values being truncated. Does anyone know where the bug is?
//This functions takes in an array as a parameter, as well as the
//total number of elements. It then calculates and returns the average.
double mean(double a[], int numelements)
{
double sum = 0.0;
double average = 0.0;
for (int i = 0; i < numelements; i++)
{
sum += a[i]; //add up sum of all elements
}
average = sum / numelements;
return average;
}
//This function takes in an array as a parameter, as well as the
//total number of elements. It then calculates and returns the standard deviation.
double standarddeviation(double a[], int numelements)
{
double avg = mean(a, numelements);
double sum = 0.0;
for (int i = 0; i < numelements; i++)
{
sum += pow((a[i] - avg), 2);
}
double variance = sum / numelements;
double stddev = sqrt(variance);
return stddev;
}