/** Simple program to illustrate the use of arrays. Here we ask the user * to enter 10 values, and we find the average, standard deviation, * max and min values. */ import java.util.Scanner; public class Array { public static void main(String [] args) { Scanner kbd = new Scanner(System.in); // declare an array "a" of 10 real numbers double [] a = new double[10]; // initialize by asking for input int i; for (i = 0; i < 10; ++i) { System.out.print("enter score #" + (i+1) + " --> "); a[i] = kbd.nextDouble(); } //////////////////////////////////////////////////////////////////// // First we need to find the average before the standard deviation. double sum = 0.0; for (i = 0; i < 10; ++i) sum += a[i]; double average = sum / 10; //////////////////////////////////////////////////////////////////// // The formula to find the standard deviation is as follows: // For each score, find its deviation (difference) from the average, // square it, add these squared values, divide by the number of // values, then take the square root. sum = 0.0; for (i = 0; i < 10; ++i) { double diff = a[i] - average; sum += diff * diff; } double standardDeviation = Math.sqrt(sum / 10); //////////////////////////////////////////////////////////////////// // Now find the extremes of the array. double max = a[0]; for (i = 1; i < 10; ++i) if (a[i] > max) max = a[i]; double min = a[0]; for (i = 1; i < 10; ++i) if (a[i] < min) min = a[i]; //////////////////////////////////////////////////////////////////// // output System.out.printf("average = %.2f\n", average); System.out.printf("Standard deviation = %.2f\n", standardDeviation); System.out.printf("max value = %.2f\n", max); System.out.printf("min value = %.2f\n", min); } }