Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help with my standard deviation code.

1. ## Need help with my standard deviation code.

Here is the data set that is given as an example:

1 2 3 4.5 5.6 6 7 8 9 10

They give the answer as: 2.99794

Here is my code:
```import java.util.*;
public class Exercise05_21 {
public static void main(String [] args){
Scanner s = new Scanner(System.in);

System.out.print("Enter ten numbers: ");

double[]x = new double[10];

int i;

for(i = 0;i < 10; i++){

x[i] = s.nextDouble();
}

double mean = mean(x, i);

double deviation = var(x);

System.out.println("The mean is " + mean);

System.out.println("The standard deviation is " + deviation);

}

public static double sum(double[] a) {
double sum = 0.0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}

public static double mean(double[]x, double i){

if (x.length == 0) return Double.NaN;
double sum = sum(x);
return sum / x.length;
}

public static double var(double[] x) {
if (x.length == 0) return Double.NaN;
double avg = mean(x, 10);
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
sum += (x[i] - avg) * (x[i] - avg);
}
return sum / (x.length - 1);
}

}```

I am not sure if my code is just wrong, if I am using the incorrect formula for standard deviation, or both. I've been working on this all freakin day and my head is about to explode. I suck at this stuff. If anyone can help I'd really appreciate it.

2. ## Re: Need help with my standard deviation code.

`return sum / (x.length - 1);`
You forgot to take the square root.

3. ## The Following User Says Thank You to helloworld922 For This Useful Post:

drew.field (April 16th, 2011)

4. ## Re: Need help with my standard deviation code.

That did it! Thanks so much.