Markus Wallace - 1 month ago 8
Java Question

# what is wrong with my standard deviation calculation?

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 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();

}
}
``````

``````import java.util.ArrayList;
import java.util.Scanner;

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);
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();

}
}
``````