Hi friends, I'm a little giddy about having solved this one all by myself, and with two asterisks to its name, I'll post this for posterity's sake.

Note the outputs within and after the loop. I have gone beyond the problem to check for myself if the program flow was correct.

Also, because Blogger messes up the formatting of my code, you might not be able to appreciate it as "real" code.

5.21** (Computing mean and standard deviation) In business applications, you are often asked to compute the mean and standard deviation of data. The mean is simply the average of the numbers. The standard deviation is a statistic that tells you how tightly all the various data are clustered around the mean in a set of data. For example, what is the average age of the students in a class? How close are the ages? If all the students are the same age, the deviation is 0. Write a program that generates ten random numbers between 0 and 1000, and computes the mean and standard deviations of these numbers using the following formula:

//MeanAndStandardDeviation.java

public class MeanAndStandardDeviation {

public static void main(String[] args) {

double count = 10.0;

double dataSum = 0;

double dataSquare = 0;

for (double i = 1.0; i <= count; i++) { double sampleData = Math.random()*1000; double squareData = Math.pow(sampleData, 2); dataSum += sampleData; dataSquare += squareData; System.out.println(sampleData + ", " + squareData); } double mean = dataSum / count; double j = ((dataSquare - ((dataSum * dataSum)/count)) / (count - 1)); double standardDeviation = Math.sqrt(j); System.out.println(dataSum + ", " + dataSquare); System.out.println(j); System.out.print("The mean of 10 random doubles is " + mean + " and the standard deviation is " + standardDeviation + "."); } }

## No comments:

Post a Comment