Advertisement
luliu

Problem 7.11 Computing Deviation

Nov 19th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. /*
  2.  * Lu Liu
  3.  * 11/19/2015
  4.  * CSCI-111-D01
  5.  * Problem 7.11 on page 278
  6.  * Computing Deviation
  7.  */
  8. package chapter07;
  9.  
  10. import java.util.Scanner;
  11.  
  12. public class ComputingDeviation {
  13.     public static void main(String[] args) {
  14.         Scanner input = new Scanner(System.in);
  15.         double[] numbers = new double[200];
  16.         System.out.println("Enter your numbers: ");
  17.         for (int i = 0; i < numbers.length; i++) {
  18.             numbers[i] = input.nextDouble();
  19.         }
  20.         displayArray(numbers);
  21.         System.out.println("The mean is " + mean(numbers));
  22.         System.out.println("The deviation is " + deviation(numbers));
  23.     }
  24.  
  25.     public static double mean(double[] x) {
  26.         double sum = 0;
  27.         for (int i = 0; i < x.length; i++)
  28.             sum += x[i];
  29.         return sum / x.length;
  30.     }
  31.  
  32.     public static double deviation(double[] x) {
  33.         double mean = mean(x);
  34.         double sum = 0;
  35.         for (int i = 0; i < x.length; i++)
  36.             sum += (x[i] - mean) * (x[i] - mean);
  37.         return Math.sqrt(sum / (x.length - 1));
  38.     }
  39.  
  40.     public static void displayArray(double[] x) {
  41.         for (int i = 0; i < x.length; i++) {
  42.             if ((i + 1) % 20 == 0)
  43.                 System.out.println(x[i]);
  44.             else
  45.                 System.out.print(x[i] + " ");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement