2048290348089 - 2 years ago 68
Java Question

# Trying to figure out how to get the standard deviation and mean with numbers aquired from Scanner with Arrays

This is one of the first assignments I have used with Arrays so I am not exactly sure how this works or if I am doing it correctly. Here is the program currently

``````public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double[] numbers = new double[9];
System.out.print("Enter 10 numbers: ");
for (int i = 0; i < 10; i++){
double number = s.nextDouble();
}

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

}

public static double deviation(double[] x) {
double mean = mean(x);
double squareSum = 0;

for (int i = 0; i < x.length; i++) {
squareSum += Math.pow(x[i] - mean, 2);
}
return Math.sqrt((squareSum) / (x.length - 1));
}

public static double mean(double[] x) {
double sum = 0;
for (int i = 0; i < x.length; i++) {
sum += x[i];
}
return sum / x.length;
}
}
``````

What have I done wrong, the output is coming out as 0.0 for both mean and standard deviation

• If you want to put 10 double values in array, you have to define the size of your array (number of elements in the array) to be 10:

``````double[] numbers = new double[10];
``````
• how do I put the inputs with the s.nextDouble go into the array?

``````for (int i = 0; i < 10; i++){
double number = s.nextDouble();
}
``````

assign the the double values you read into the array as:

``````for (int i = 0; i < numbers.length; i++){
numbers[i] = s.nextDouble();
}
``````