Sunday, July 12, 2009

C++ array function involving standard deviation?

I need a function that takes two arguments: first argument is an array of numbers and the second argument is the size of the array, and the function returns the standard deviation of those numbers (all of type double).

C++ array function involving standard deviation?
#include %26lt;math.h%26gt;





double stddev(const double input[], size_t length)


{


double variance=0.0f;


double avg=0.0f;


size_t idx;


for (idx=0; idx%26lt;length; idx++)


{


avg+=input[idx] ;


}


avg/=length;


for (idx=0; idx%26lt;length; idx++)


{


variance+=(input[idx]-avg) * (input[idx]-avg);


}


variance/=length;


return sqrt(variance);


}

snapdragon2

No comments:

Post a Comment