package chapter5;

/**
 * Title:        Chapter 5, "Arrays"
 * Description:  Examples for Chapter 2
 * Copyright:    Copyright (c) 2000
 * Company:      Armstrong Atlantic State University
 * @author Y. Daniel Liang
 * @version 1.0
 */
// Deviation.java: Compute deviation

public class Deviation
{
  public static void main(String[] args)
  {
    // Declare and create an array for 10 numbers
    double[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Print numbers
    printArray(numbers);

    // Display mean and deviation
    System.out.println("The mean is " + mean(numbers));
    System.out.println("The standard deviation is " +
      deviation(numbers));
  }

  /** Method for computing deviation */
  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);
  }

  /** Method for computing mean */
  public static double mean(double[] x)
  {
    double sum = 0;

    for (int i=0; i<x.length; i++)
      sum += x[i];

    return sum/x.length;
  }

  /** Method for priting array */
  public static void printArray(double[] x)
  {
    for (int i=0; i<x.length; i++)
      System.out.print(x[i] + " ");
    System.out.println();
  }
}