Write a program that prompts the user for 10 floating point values. The program
ID: 3806753 • Letter: W
Question
Write a program that prompts the user for 10 floating point values. The program should invoke two methods to calculate the mean and standard deviation of the numbers. You can use the formula below to calculate the mean and standard deviations. mean = sigma^n_i = 1 x_i/n = x_1 + x_2 + ... + x_n/n deviation = Squareroot sigma^n_i = 1 (x_i - mean)^2/n - 1 You will need: 1. A scanner object to read the 10 values 2. An array to store the ten values 3. Two methods that do the following: 1. Accepts an array and returns the mean 2. Accepts an array and returns the standard deviation 4. Comments where necessary A sample of the output is shown below: Enter ten numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2 The mean is 3.1100000000000003 The standard deviation is 1.5573838462127583Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
double numbers[] = new double[10];
double mean=0,sd=0;
System.out.println("Enter ten numbers:");
for(int i=0;i<10;i++){
numbers[i] = sc.nextDouble();
}
for(int i=0;i<10;i++){
mean += numbers[i]; // Total of the numbers
}
mean /= 10; // Average
System.out.println("The mean is " + mean);
for(int i=0;i<10;i++){
sd += Math.pow(numbers[i]-mean,2); //Doing the summation
}
sd /= 9; // divide by n-1
sd = Math.sqrt(sd); // doing the square root of variance
System.out.println("The standard deviation is "+sd);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.