# Thread: standard deviation error

1. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## 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:

Java 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);```  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## 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.  Reply With Quote

3. Member Join Date
Mar 2011
Posts
16
Rep Power
0

## Hmmm, I've checked all of that. Here's my code:

Java 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);

}
}```  Reply With Quote

#### Tags for this Thread

errors, math, standard deviation #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•