-2

my code to find standard deviation is wrong. my code is supposed to find standard deviation from user input. i typed in the numbers 1 2 3 and the standard deviation of this set of numbers is 1 but it printed 10 where did i go wrong. also i know i have a bunch of unused variables dont mind them.

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 i;
        int count=0; 
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE; 
        double average=0;
        int sum;
        double deviation = 0;
        int total;
        int temp = 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;
          temp += Math.pow(n - average, 2);

        } 

        deviation = temp
        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);
        System.out.printf("The standard deviation of the list is %d\n", temp);

        input.close(); 


    } 
}
  • Within the loop, `average` is not actually the average, it is the cumulative sum. I think you need to calculate the deviation after you have the actual average – OneCricketeer Oct 09 '16 at 06:19
  • Your code is doing exactly what you have written it to do, but your method of calculating the standard deviation is quite wrong. Have a read through this page and then rewrite your code (significantly) [Calculating Standard Deviation](https://www.strchr.com/standard_deviation_in_one_pass) – Erwin Bolwidt Oct 09 '16 at 06:20
  • what is my code doing? i thought it was taking the first input number and subtracting the average then squaring them – Markus Wallace Oct 09 '16 at 06:35
  • Ermm ... your code is calculating something that is not the standard deviation. Read the page that Erwin linked to. Compare the code in that page to your code. – Stephen C Oct 09 '16 at 06:36
  • For a start, you are subtracting the average before you have calculated it (properly)! – Stephen C Oct 09 '16 at 06:39
  • *"also i know i have a bunch of unused variables dont mind them."* - Sorry, but that is the wrong attitude. You should clean up the cruft *before* you ask someone to read your code. For a start, the cruft *could* be the actual cause of your problems. – Stephen C Oct 09 '16 at 06:42
  • but its not cause it compiles if it were the main problem it wouldn't be in there obviously – Markus Wallace Oct 09 '16 at 06:50

2 Answers2

1
import java.util.ArrayList;
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 = 0;
        int i;
        int count = 0;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        double average = 0;
        int sum;
        double deviation = 0;
        int total;
        double temp = 0;// correction here because it must store double or float value

        ArrayList<Integer> n1 = new ArrayList<Integer>();// i used this to store all entered values

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

        }

        average = average / count; // this will give you final average

        for (int j = 0; j < count; j++) {
            temp += Math.pow(n1.get(j) - average, 2);
        }
        //System.out.println("\n" + temp + " " + count);
        deviation = Math.sqrt(temp / count); // this is your standard deviation

        //System.out.println(deviation);

        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);
        System.out.println("The standard deviation of the list is " + deviation);

        input.close();

    }
}
Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20
V. Mishra
  • 61
  • 4
0

Standard deviation for one variable is defined here. You have to take the square root of the mean of the sum of the squared differences between observations and the mean of the set.

//in the loop
temp += Math.pow(n - average, 2)
//outside the loop
deviation = Math.pow(temp/count,0.5) //or alternatively Math.sqrt()

This should give you what you need.