Lab 13: Statistical operations on arrays

Goals

Demonstrate proficiency in: (1) computing the mean and variance of a collection of floating point values and (2) renormalizing the collection in such a way that the renormalized collection has a pre-specified target mean and target variance.

Background

The mean of a collection of values

The mean is simply another name for the average. To compute the mean just sum the values and divide the sum by the number of values.

The variance of a collection of values

The variance is a measure of the variablily of a collection. If all the values in a collection are the same, the variance is 0. The collection {-10, 0, 10} and the collection {-1, 0, 1} both have mean zero but the {-10, 0, 10} has a much larger variance.

The variance is mean of the squares of the differences between individual values and the mean of the collection. Given the collection {1, 3, 5} the mean is (1 + 3 + 5) / 3 = 3. The variance is ((1 - 3)2 + (3 - 3)2 + (5 - 3)2 ) / 3 = (4 + 4) / 3 = 8 / 3.

The standard deviation is the square root of the variance.

Normalization

A arbitrary collection of values may be converted to a collection having mean 0 by subtracting the mean of the original collection from each value.

A collection of values having mean 0 but arbitrary variance can be converted to a collection having mean 0 and variance 1 by dividing each value in the original collection by the standard deviation of the original collection.

A collection having mean 0 and variance 1 can be converted to a collection having mean 0 and and an arbitrary target variance, targvar, by multiplying each value in the original collection by the square root of targvar.

A collection having mean 0 can be converted to a collection having an arbitrary target mean, targmean, by adding targmean to each element in the original collection.

Therefore, a collection having an arbitrary mean and standard deviation may be linearly transformed into a collection having a different target mean and standard deviation by application of these four steps.

Assignment:

Your mission is to write a program that can read in a collection of values from the standard input, a target mean and variance from the command line, and transform the collection of values so that instead of its original mean and variance it has the target mean and target variance.

Program lab13.c in the lab13 subdirectory should be used as a starting point for this assignment.

Specific tasks include:

Add code to the main function to use sscanf() to read the target mean and variance from the command line. Then add the code needed to read the input values from the standard input.

Write the float mean() function that computes the mean of the values.

Write the float var() function the computes the variance of the values.

Write the function void normalize() that renormalizes the entire collection so that it has the target mean and variance. This can be done in a single loop in which the four steps described on the previous page are applied to each element in the array.

Use of subscript instead of pointer notation in manipulating the collection of values will lead to a deduction.

Turn In Work

Show your TA that you completed the assignment. Then turn in your lab13.c program using the command:

sendlab.101.section_number 13 lab13.c

2

Mike Westall

2007-09-03T18:26:51