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.