Prelab 7: Working with Arrays


In lab 7, you will be working with arrays and writing functions that manipulate them.

Study the program ArrayStats.java carefully and be sure how it functions. Think of the changes that need to be made to the methods min, max and average if they were to operate on an integer array instead of double.

You are required to add one more method to the existing ArrayStats applet which calculates the standard deviation of the numbers in the array.

Standard Deviation is a measure of the spread or extent of variability of a set of scores (set of numbers) around their mean. The standard deviation reflects the degree of homogeneity of the group with respect to the variable in question. That is, the less the dispersion of scores, the smaller will be the standard deviation.

A very trivial example of is is a set of equal numbers (i.e. all the numbers in the array are the same, say.. list[] ={ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2} ).
There is NO dispersion of the above numbers from what is the mean of these numbers (i.e. 2). Hence the standard deviation of the above set of numbers (array) is ZERO! 

Calculating Standard Deviation:

Terms you'll need to know
x = one value in your set of data
avg (x) = the mean (average) of all values x in your set of data
n = the number of values x in your set of data

For each value x, subtract the overall avg (x) from x, then multiply that result by itself (otherwise known as determining the square of that value). Sum up all those squared values. Then divide that result by (n-1). Got it? Then, there's one more step... find the square root of that last number. That's the standard deviation of your set of data.

For example if the list of numbers is 1, 2, 3, 4 then the mean (average) is 2.5  which we call as avg(x) . Using the above steps to calculate Standard deviation will result in 1.29099445.

If you have already seen a formula which does this automatically, you will reach the same answer. But the above description is meant to help you write a function which calculates standard deviation of an array of numbers for you.

Write a function which finds the standard deviation of an array of integers.

Write a stdDev method (on paper) that takes an array of doubles as a parameter and returns the standard deviation of the values in the array.

Also think how would you integrate your standard deviation function with ArrayStats.java.


Siddhesh Sarvankar (siddhesh@cs.duke.edu) http://www.cs.duke.edu/courses/fall05/cps001/