Edmonds Community College
Computer Science   
 CS &131
Printer-Friendlier Version

Project 2:

NAME: "Standard Deviation"

Practice using Top-down design, formula calculations and formatted sequential file I/O.
Standard deviation is a measure of how much data values in a collection deviate from the average value. Your task is to write an ANSI C program which takes a data list in from a file and finds the standard deviation of the data.

To do this, you will first need to have a file set up in the appropriate format. Some test files will be provided. The program should ask the user the name of the file to be opened for reading, and if it is possible, open it for reading; otherwise the program should end with an appropriate message to the screen. The program should also ask for and input the name of the file to which to write the results. Open it for writing (not appending), if possible; otherwise close the input file and exit with an appropriate message to the screen

From the input file the program should read in, from a single line, the total number of elements to be entered into the list. This value will then be used to set up a for loop for reading in the data samples. By this method, the program will then read in a list of integer data, one after the other from a single line in the file, with the data separated by spaces.  This data will only be read into a single variable, that is, when one datum is read in, the old datum will be wiped out. Do NOT use an array to store this data!

While the program is reading in the data, it will also be collecting calculations. One variable will keep track of the smallest data piece so far. Another will track the largest number. Yet another will track a summation of all the data so far, and still another will track a summation of the squares of the data.

After the loop is finished, the mean (average) and an approximation of the variance can be calculated by the formulae (where n is the number of elements):

mean      =  sum_of_data / number_of_elements
variance =
(sum_of_the_squares - (sum_of_data2 / number_of_elements)) / ( number_of_elements - 1)

The standard deviation is the square root of the variance.

Next, the program should print out the results to a new file. It should print, in tabular form:

  • the total number of elements
  • the summation of values
  • the range of values (the lowest and highest)
  • the mean value (to 3 places)
  • the variance (to 3 places), and
  • the standard deviation (to 3 places).

The table should have fixed column widths, as in the example below.

If there is no more information in the files, the program should close both files, send a goodbye message to the screen, and exit.  If there is more data, the program should read in the number of additional elements to be read in (on its own line), and read in the space-separated data as before. After calculations are made, a new line will be added to the table in the output file showing the statistics for all of the data read in to this point (see the example below. Note that the second line starts with 7 <i.e., 5 + 2> -- a running total).  This process should continue until there are no more data in the file.

N.B.: Do not allow division by zero. If there are no data, set the mean to 0. If there are fewer than 2 data, set the variance and standard deviation each to 0.


Section A:

Thursday, February 9, 2017, 10:30 a.m.
(beginning of class)
The program should be fully planned in advance. It should be well documented, and work correctly.

If the input file dat1.dat looks like this:

5  7  3  6  6                
2  5
Then the output file should look something like:
The results from file dat1.dat are:
|# of data|  sum  |   range   | mean | variance | standard deviation|
|        5|     27|     3 to 7| 5.400|     2.300|             1.517 |
|        7|     34|     2 to 7| 4.857|     3.143|             1.773 |

Notice that the addition of 2 new pieces of data shows in the results as a total of 7.

Test your program using:

  • an empty file
  • a file containing just the number "0"
  • a file containing just a "1" and one negative datum
  • at least the three given test files.

A running version of this program, with three sample test files, is on Canvas in Stddev.zip. You may use this program to compare answers in your test plan.

[red line]

Physical: this assignment only--none


Due: Section A: Thursday, February 9, 2017, 10:30 a.m. (beginning of class)

Computer Science  
Edmonds Community College  
For Further information, contact pbladek@edcc.edu

copyrightę2001-2017 Edmonds Community College