JamesMadisonUniversity

Computer Science Department


CS 139 Lab:Computation of the Standard Deviation


Objectives:

Practice using an array to solve a real problem.

Background:

Standard deviation is a measure of the spread of values around a mean. The higher the standard deviation, the more the values are scattered away from the mean. To calculate, one must first calculate the mean (average) and then use that mean to calculate the distance of each value from the mean. These distances are squared to account for numbers both less than and greater than the mean value.

From Wikipedia:

formula

In other words, the standard deviation of a discrete uniform random variable X can be calculated as follows:

1. For each value xi calculate the difference xi - x between xi and the average value x.

2. Calculate the squares of these differences.  Sum the squares of the distances.

3. Find the average of the squared differences. This quantity is the variance σ2.

4. Take the square root of the variance.

New Terms:

Command line argument

Mean

Standard deviation

Materials:

You will write this program from scratch.

You may use the following file for testing: testData.

To use the file, run your program in the command line environment and override standard input. See demo at the beginning of class for this technique.

Submit is available for this lab.

Acknowledgment Adapted from a lab by Nancy Harris

1 General Instructions:

  1. Setup your program environment on your n-drive.
  2. Download the testData file.
  3. Create a new program file StdDev.java, and StdDevHelper.java.

2 Mean calculation:

  1. You will implement your program in two files.  
  2. The main function (in StdDev.java) will read in a series of numbers, create an array containing those numbers and then call the mean and standard deviation functions from the other file, reporting on the results.
    1. If the main program is passed a command line argument, that argument will specify the number of values to be read. If no command line argument is passed, the program should read 25 values.Realize that the argument list is an array of Strings. (see theInteger.parseInt() documentation for converting a string to an integer).
    2. Create an array of double that is built to the size of the command line argument or 25 as a default if no argument is provided.
    3. To read in the values, you will have a loop which will read the value from the file (as doubles) and store them into the array. No prompt should be output before reading the values.
    4. Once the array is built, you will pass it to functions in StdDevHelper.java.
    5. You should output the values as shown below in Output.
  3. In the StdDevHelper.java file you will create a function (static method). mean, will take in an array of values and calculates and returns the mean(average) of those numbers. Your method should insure that the array is not null and that it has more than 0 elements. If either of those conditions is true, return 0.
  4. The only output from the program will be a single line using format "Mean: %.2f\n" containing the calculated mean value.This output line will be placed in the StdDev.java program.
  5. Use submit to test this program. You will achieve 50% of the points for successfully testing the mean method.

3 Standard deviation:

Add to your Helper file a function, stdDev, that computes the standard deviation of a passed array of double numbers. See the formula above. Your standardDeviation function should use the mean function created in Part 2 above.Again, you should insure that the array is not null and that it has more than 0 elements. If either of those two conditions is true, return 0.

  1. The program's output will be the mean on one line as shown above and on the next line the standard deviation using format string "Standard Deviation: %.2f\n".
  2. Use the submit system to test your program, submitting it to L09-02stdDev. You will achieve 100% of the points for a successful submission. It is not necessary to separately test the mean calculation if you don't wish to.

Updated 11/09/11 (nlh)