This program will prompt the user to enter 10 numbers. You are supposed to store
ID: 3730172 • Letter: T
Question
This program will prompt the user to enter 10 numbers. You are supposed to store the numbers in an array of doubles. You are then required to calculate the mean, variance and standard deviation of the entered values. Make sure your program conforms to the following requirements. • This program doesn’t need separate methods. You can write it all in main() if you wish. • Mean is defined as the average of all the entered values. You need to calculate mean first, as variance depends on mean and standard deviation depends on variance. • Variance is defined as the average of the squares of the distance from the mean. For each value in the array, subtract the value from the mean, square the result and then add the square to a running total. Divide that by the number of values (10). • Standard deviation is defined as the square root of the variance. This should be simple enough. • Print all the results rounded to 2 decimal places. For example, if your data set was just {4, 7.5, 8}, then the mean is (4 + 7.5 + 8)/3 = 19.5/3 = 6.5. Variance = ((6.5 - 4)2 + (6.5 - 7.5)2 + (6.5 - 8)2 )/3 = (6.25 + 1 + 2.25)/3 = 9.5/3 = 3.17 Standard Deviation = 3.17 = 1.78
This program has to done in Java
Explanation / Answer
MeanVarianceStdDev.java
import java.util.Scanner;
public class MeanVarianceStdDev {
public static void main(String[] args) {
double values[] = new double[10];
//Declaring variables
double sum1 = 0.0, mean;
double variance, std_dev, sum = 0.0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
for (int i = 0; i < values.length; i++) {
//Getting the input entered by the user
System.out.print("Enter the value#" + (i + 1) + ":");
values[i] = sc.nextDouble();
}
//calculating the sum of nos[] array elements
for (int i = 0; i < values.length; i++) {
//calculating the sum
sum1 += values[i];
}
//calculating the average
mean = sum1 / values.length;
for (int i = 0; i < values.length; i++) {
sum += Math.pow(values[i] - mean, 2);
}
//calculating the standard deviation of values[] array
variance = sum / (values.length - 1);
double stdDev = Math.sqrt(variance);
//Displaying the output
System.out.printf(" Mean :%.2f", mean);
System.out.printf(" Variance :%.2f", variance);
System.out.printf(" Standard Deviation :%.2f", stdDev);
}
}
__________________
Output:
Enter the value#1:45.56
Enter the value#2:67.76
Enter the value#3:65.54
Enter the value#4:43.34
Enter the value#5:32.23
Enter the value#6:67.89
Enter the value#7:90.98
Enter the value#8:88.77
Enter the value#9:66.55
Enter the value#10:22.23
Mean :59.09
Variance :517.66
Standard Deviation :22.75
__________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.