CS21 Homework 7
Due Sunday, October 19 by 11:30pm
Note that this is due at the end of Fall break.
Introduction
In this assignment you will practice using arrays to process lists of
numbers. This assignment has been adapted from programming exercises
11.3, 11.4, and 11.7 from our textbook The Art and Science of
C. You should work on your own for this assignment.
Statistical Analysis
A primary purpose of statistical analysis is to find ways to compress
a set of data into summary information that expresses properties of
the data. The most common statistical measure is the mean, which is
simply the average. Another common statistical measure is the
standard deviation, which provides an indication of how much the
individual values in the data differ from the mean. Finally, a
histogram is a graphical way of summarizing data by dividing it into
separate ranges and then indicating how many data values fall into
each range. The histogram portion of this
assignment is optional; you may do this for extra credit if you are
interested.
Here is a sample run of a program that performs these statistical
analyses:
This program will analyze a list of integers by calculating
the mean and the standard deviation. It will also print a
histogram of the values. It expects the values to be integers
between 0 and 100.
Enter integers to be stored, -1 to end.
? 90
? 89
? 97
? 95
? 123
Values should be between 0 and 100. Try again.
? 84
? 71
? 78
? 88
? 82
? 100
? 55
? -1
Mean: 84.4545
Standard Deviation: 12.3241
Histogram:
0 - 9 |
10 - 19 |
20 - 29 |
30 - 39 |
40 - 49 |
50 - 59 | *
60 - 69 |
70 - 79 | **
80 - 89 | ****
90 - 99 | ***
100 - 100 | *
Program Requirements
- You should modify the function GetIntegerArray from
chapter 11, so that it takes two additional parameters which indicate
the minimum and maximum range of acceptable values. The new prototype
should be:
int GetIntegerArray(int array[], int n, int min, int max, int sentinel);
For example, GetIntegerArray(scores, 50, 0, 100, -1) will
read integers into an array called scores, which can hold 50
numbers whose values are between 0 and 100, until the sentinel value
of -1 is entered. It will return the number of values read in.
- To calculate the mean, you should use a function with the
prototype:
double Mean(int array[], int n);
that returns the real-valued mean of an array of type int
whose effective size is n.
- To calculate the standard deviation, you should use a function
with the prototype:
double StandardDeviation(int array[], int n);
that returns the real-valued standard deviation of an array of type
int whose effective size is n. In order to
calculate the standard deviation, you need to perform the following
steps:
- Calculate the mean of the list of numbers.
- Go through the individual data items in the list and calculate the
square of the difference between each data value and the mean. Add
all these values to a running total.
- Take the total from the previous step and divide it by the number
of items in the list.
- Calculate the square root of the resulting quantity, which
represents the standard deviation. Note that the math.h
library has a square root function called sqrt.
- If you decide to work on the histogram portion, then your program
should generate output that looks as much like the sample run as
possible. The number of asterisks in a particular row indicates the
number of values in the list that fell into each designated range.
Handing in your solution
Use cs21handin to turn in your program.