Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Omar Natour
- * 11/23/2015
- * Csc-111 D01
- * Introduction to Java
- * Problem Number 7.11
- * email Ojnatour0001@student.stcc.edu
- * Problem Desc: Output the mean and standard deviation of values in an array. Currently optimized for use in CMD
- */
- import java.util.Scanner;
- import java.lang.Math;
- public class StandardDev {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- //System.out.print("Enter in the length of your array:"); /* Uncomment for use in eclipse console */
- int n = input.nextInt();
- double arr[] = new double[n];
- //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 */
- for (int i = 0; i < n; i++)
- arr[i] = input.nextDouble();
- System.out.println("The mean of your array is " + mean(arr));
- System.out.println("The Standard deviation of your array is " + deviation(arr));
- System.out.println("Your array looks like:");
- outp(arr);
- input.close();
- }
- public static double mean(double[] y) {
- int n = y.length;
- double total = 0;
- double ave = 0;
- for (int i = 0; i < n; i++)
- total += y[i];
- ave = total / n;
- return ave;
- }
- public static double deviation(double[] x) {
- double ave = mean(x);
- double sig = 0;
- for (int i = 0; i < x.length; i++) {
- sig += ((x[i] - ave) * (x[i] - ave)) / x.length;
- }
- return Math.sqrt(sig);
- }
- public static void outp(double[] a) {
- for (int i = 0; i < a.length;) {
- System.out.printf("%8.1f", a[i]);
- i++;
- if (i % 20 == 0)
- System.out.print("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement