I'm working on a program for school that involves finding certain statistics of an array, and one of the things I have to find is the variance, I have a function made for it but for some reason it keeps returning 0
double variance(double * array, unsigned int size, double value)
{
double variance = 0; //Variable used to store the variance
for (unsigned int i = 0; i < size; ++i)
{
if (value > array[i]) //If statement checks to see if you need to subtract the value from the array or the other way around
{
variance += pow((value - array[i]), 2);
}
else
{
variance += pow((array[i] - value), 2);
}
}
variance /= size;
return variance;
}
I've tried going through it with the debugger but I came out with nothing
how I called it:
double varaince = variance(array, size, mean);
printf("\nVariance: %.3lf", variance);
As @ammoQ commented
double varaince = variance(array, size, mean);
// function name used here.
// printf("\nVariance: %.3lf", variance);
// Use variable name
printf("\nVariance: %.3lf", varaince);
variance/=size;
Should be
variance/=(double)size;
size on its own is stored as a binary integer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.