Markus Wallace
Markus Wallace

Reputation: 13

how to get the standard deviation

idk if i have to get the standard while in the loop or out of the loop.also can you help me understand why it is i do it inside the loop or out and what the difference is. also i know the standard deviation formula in this situation would be something like (input - average)^2 for the first then ++ for every value after then add all that up and divide by the count then square root that. im just not fully sure how to write it and where to put it

import java.util.Scanner; 

public class readFromKeyboard { 

    public static void main(String[] args) { 


        Scanner input = new Scanner(System.in); 

        String inStr = input.next(); 
        int n; 
        int count=0; 
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE; 
        double average=0;
        int sum;
        double deviation = 0;

        while (!inStr.equals("EOL")) { 
          count++; 
          n = Integer.parseInt(inStr); 
          min = Math.min(min, n);
          max = Math.max(max, n);
          System.out.printf("%d ", n); 
          inStr = input.next(); 
          average += n;
        } 

        average = average/count;

        System.out.println("\n The average of these numbers is " + average);
        System.out.printf("The list has %d numbers\n", count); 
        System.out.printf("The minimum of the list is %d\n", min);
        System.out.printf("The maximum of the list is %d\n", max);


        input.close(); 


    } 
}

Upvotes: 0

Views: 102

Answers (1)

drum
drum

Reputation: 5651

Given that you already calculated average, now you can calculate the standard deviation for each number.

  1. Create array sd[] to store the standard deviation.
  2. For each number, sd[i] = (average - input_i) ^ 2

Calculate variance:

  1. For each standard deviation in sd[], add to a variable temp
  2. Divide temp by total number of inputs

Calculate population standard deviation:

  1. Square root variance

Upvotes: 1

Related Questions