# standard deviation error

• 05-03-2011, 04:52 AM
rochla16
standard deviation error
My standard deviation is off, and I'm not sure why. I found this formula online, but there must be something wrong...Anyone have ideas? :confused:

Code:

```double deviation=0; for (int i = 0; i < numbers.length; i++) { sum += Math.pow((numbers[i] - average), 2); } deviation = Math.sqrt(sum /(number_of_elements-1)); System.out.println ("The SD is : " + deviation);```
• 05-03-2011, 05:31 AM
pbrockway2
Check that you are initialising sum to zero and that number_of_elements has the correct value. Also check that you are supposed to subtract one from it. (there are different ways of defining it). And check that average has the value you expect.

If you still get a strange result post a Short, Self Contained, Correct Example: something that others can run as well as a description of the result you expected.
• 05-03-2011, 05:43 AM
rochla16
Hmmm, I've checked all of that. Here's my code:

Code:

```public class sample { public static void main (String[]args){ java.util.Scanner input = new java.util.Scanner(System.in); final int number_of_elements=10; double [] numbers = new double [number_of_elements]; double sum=0; for (int i=0; i < number_of_elements; i++) { System.out.print ("Enter a number: "); numbers[i] = input.nextDouble(); sum+=numbers[i]; } double average = sum/number_of_elements; System.out.println ("The mean is " + average); double deviation=0; for (int i = 0; i < numbers.length; i++) { sum += Math.pow((numbers[i] - average), 2); } deviation = Math.sqrt(sum /(number_of_elements-1)); System.out.println ("The SD is : " + deviation);   } }```