Practice using Top-down design, formula calculations and formatted sequential file I/O.
deviation is a measure of how much data values
in a collection deviate from the average value. Your task is to write
a 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 exists, 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. If it exists, open it for writing; otherwise 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):
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 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.
February 13, 2014, 10:30 a.m.
(beginning of class)
|The program should be fully planned
in advance. It should be well documented, and work
If the input file dat1.dat looks like this:
Notice that the addition of 2 new pieces of data shows in the results as a total of 7.
Test your program using:
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.
Thursday, February 13, 2014, 10:30 a.m.
(beginning of class) a.m. (beginning of class)