Reputation: 13
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
Reputation: 5651
Given that you already calculated average
, now you can calculate the standard deviation
for each number.
sd[]
to store the standard deviation
.sd[i] = (average - input_i) ^ 2
Calculate variance
:
standard deviation
in sd[]
, add to a variable temp
temp
by total number of inputsCalculate population standard deviation
:
variance
Upvotes: 1