# Standard Deviation of an array of numbers...

• 11-19-2003
Xenofizz
Standard Deviation of an array of numbers...
I am writing a program that tells the user to enter 20 numbers, then it uses those and finds the average and standard deviation.

I put the 20 numbers in an array.

Does anyone know the c++ formula for standard deviation of an array? It's blowing my mind. I've been trying for hours.
• 11-19-2003
JaWiB
I don't know what "standar deviation" is...someone else here might, but in the meantime it would help if you could explain it a bit.

Edit: after a google search I found one explanation:

standard deviation is the square root of the variance of a mean of a set of numbers

so for the set 1,2,3 the variance is:

( (1-avg)+(2-avg)+(3-avg) ) / n

in this case avg is 2 and n is 3

it also went on to say that you should divide by n-1 to give an unbiased estimate...although it didn't really explain why

If this is correct it shouldn't be too hard to do...
• 11-19-2003
Xenofizz
It's a real weird formula.

It's the square root of a summation of each number minus the average, squared and divided by the amount of numbers minus 1.

It's hard to explain, even harder to code.
• 11-19-2003
JaWiB
Sounds like i had it somewhat correct, but I had an error, I meant to square those terms:

(1-avg)*(1-avg), etc

So you should be able to do something like:
Code:

```for (int i=0;i<n;i++)   total+=(nums[i]-avg)*(nums[i]-avg); total/=n-1; std::cout<<sqrt(total);```
• 11-19-2003
Xenofizz
Thanks!

I got it to work, I think. Even if not so, you helped a lot. Awesome!