Advertisement
Omar_Natour

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

Nov 23rd, 2015
128
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.  * email Ojnatour0001@student.stcc.edu
  7.  * Problem Desc: Output the mean and standard deviation of values in an array. Currently optimized for use in CMD
  8. */
  9.  
  10. import java.util.Scanner;
  11. import java.lang.Math;
  12.  
  13. public class StandardDev {
  14.     public static void main(String[] args) {
  15.         Scanner input = new Scanner(System.in);
  16.  
  17.         //System.out.print("Enter in the length of your array:");  /* Uncomment for use in eclipse console */
  18.         int n = input.nextInt();
  19.         double arr[] = new double[n];
  20.        
  21.         //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 */
  22.         for (int i = 0; i < n; i++)
  23.             arr[i] = input.nextDouble();
  24.  
  25.         System.out.println("The mean of your array is " + mean(arr));
  26.         System.out.println("The Standard deviation of your array is " + deviation(arr));
  27.         System.out.println("Your array looks like:");
  28.         outp(arr); 
  29.         input.close();
  30.     }
  31.  
  32.     public static double mean(double[] y) {
  33.  
  34.         int n = y.length;
  35.         double total = 0;
  36.         double ave = 0;
  37.  
  38.         for (int i = 0; i < n; i++)
  39.             total += y[i];
  40.         ave = total / n;
  41.         return ave;
  42.     }
  43.  
  44.     public static double deviation(double[] x) {
  45.  
  46.         double ave = mean(x);
  47.         double sig = 0;
  48.  
  49.         for (int i = 0; i < x.length; i++) {
  50.             sig += ((x[i] - ave) * (x[i] - ave)) / x.length;
  51.         }
  52.         return Math.sqrt(sig);
  53.     }
  54.  
  55.     public static void outp(double[] a) {
  56.         for (int i = 0; i < a.length;) {
  57.             System.out.printf("%8.1f", a[i]);
  58.             i++;
  59.             if (i % 20 == 0)
  60.                 System.out.print("\n");
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement