Omar_Natour

Natour, O. 11/23/15 Csc-111-D01 Hw 7.11

Nov 23rd, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. /*Omar Natour
  2.  * 11/23/2015
  3.  * Csc-111 D01
  4.  * Introduction to Java
  5.  * Problem Number 7.11
  6.  * Problem Desc: Output the mean and standard deviation of values in an array. Currently optimized for use in CMD
  7. */
  8.  
  9. import java.util.Scanner;
  10. import java.lang.Math;
  11.  
  12. public class StandardDev {
  13.     public static void main(String[] args) {
  14.         Scanner input = new Scanner(System.in);
  15.  
  16.         //System.out.print("Enter in the length of your array:");  /* Uncomment for use in eclipse console */
  17.         int n = input.nextInt();
  18.         double arr[] = new double[n];
  19.        
  20.         //System.out.print("Type the values for accending positions in the array starting at position 0. Press Enter after every value."); /* Uncomment for use in eclipse console */
  21.         for (int i = 0; i < n; i++)
  22.             arr[i] = input.nextDouble();
  23.  
  24.         System.out.println("The mean of your array is " + mean(arr));
  25.         System.out.println("The Standard deviation of your array is " + deviation(arr));
  26.         System.out.println("Your array looks like:");
  27.         outp(arr); 
  28.         input.close();
  29.     }
  30.  
  31.     public static double mean(double[] y) {
  32.  
  33.         int n = y.length;
  34.         double total = 0;
  35.         double ave = 0;
  36.  
  37.         for (int i = 0; i < n; i++)
  38.             total += y[i];
  39.         ave = total / n;
  40.         return ave;
  41.     }
  42.  
  43.     public static double deviation(double[] x) {
  44.  
  45.         double ave = mean(x);
  46.         double sig = 0;
  47.  
  48.         for (int i = 0; i < x.length; i++) {
  49.             sig += ((x[i] - ave) * (x[i] - ave)) / x.length;
  50.         }
  51.         return Math.sqrt(sig);
  52.     }
  53.  
  54.     public static void outp(double[] a) {
  55.         for (int i = 0; i < a.length;) {
  56.             System.out.printf("%8.1f", a[i]);
  57.             i++;
  58.             if (i % 20 == 0)
  59.                 System.out.print("\n");
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment