(Latest Revision: Fri Nov 30 20:34:45 PST 2012 )
/* ******************************************** */ /* GETDATA */ /* ******************************************** */ /* The purpose of function getData is to + read from the input file whose name is stored in: fname, + copy the values in the file into the array called: data, and + set the parameter numSlotsUsed equal to the number of items copied into the array. (Function getData copies the first item in the file, if one exists, into data[0], copies the second item into data[1], and so on.) Function getData handles the following exceptional situations: + input file cannot be opened, + input file is empty, and + input file contains more elements than the value of arraySize. In those situations, function getData prints an error message and terminates execution of the program (exits). HINTS What code do we need to implement function getData? Declare an input stream. There's a filename stored in the char array fname. Connect the stream to the file. (in the code, include an error-check for failure to open the file) (See example in Notes/Programs+Files/01_read_ints.cpp) Declare an int index variable for keeping track of which array slot gets the next value from the file. Also declare another int variable to use as the 'buffer'. Each time the program reads an integer from the file, it will copy it (first) into the buffer. Suppose that the name of the input file stream is f_in. Then a code like int nextDatum ; while (f_in >> nextDatum) ; would read through the file to the end. Something like this code: int nextDatum ; while (f_in >> nextDatum) cout << nextDatum << endl ; would read through the file AND write each of the values read from the file to the screen. If you want the code to put each element of the file into an array as it reads them, and if the name of the array happens to be my_array, then you could write the code like this: int nextDatum ; int arrayIndex = 0 ; while (f_in >> nextDatum) { my_array[arrayIndex] = nextDatum ; arrayIndex++ ; } However that way of putting numbers into the array would cause a problem if the file happens to contain more integers than there are slots in the array. The code above would continue to read through the entire (too big) file, and (this is the bad part) try to put integers into non-existent array locations - locations with indexes greater than the maximum array index. That could cause other variables in the program to be overwritten with values intended for the array, which could lead to incorrect behavior of the program However if we modified the first line in the loop body, so that line is executed only if the arrayIndex is less than the size of the array, then the code will not try to put data in non-existent array locations, but it will still read to the end of the file and count the number of integers in the file. (Notice that arrayIndex will equal the number of integers in the file after the loop finishes executing.) Once the function has a variable equal to the number of items in the file, it can execute some if-else statements that cause the program to exit if the file is empty, or if the file contains too many items. On the other hand, if the number of items in the file is OK, then the function could use this variable to give the parameter numSlotsUsed the value it is supposed to have. */ void getData (int data[ ], const char fname[ ], int arraySize, int & numSlotsUsed) { } /* ******************************************** */ /* SHOWDATA */ /* ******************************************** */ /* The purpose of function showData is to print some text to the screen, followed by the values of array items data[0] through data[numSlotUsed-1]. Function showData prints array values in a field width of 3. It prints ten values to a line, except it may print fewer values on the last line. Function showData prints the values as a comma-separated list. In particular, function showData prints a comma right after each value, except the last one. It prints a period right after the last value. Between every pair of successive items on a line, it prints a blank space right after the comma. See the file terminalSession.html for examples illustrating the remaining rules for formatting the output of function showData. HINTS On page 377 of the text, lines 21-22 are an example of outputting the values in an array to the screen. There's an example of code that writes a comma separated list of numbers here: http://www.cs.csustan.edu/~john/Classes/CS1500/LabDir/Lab04/sampLoop4.cpp.html You can use the setw() manipulator to write numbers right justified in a 3-character field. Read about it on page 325 of your text book. Concerning the problem of printing 10 numbers per line, you can test your loop counter % 10 to decide whether to output an endl or newline character. */ void showData (int data[ ], int numSlotsUsed) { } /* ******************************************** */ /* FINDMIN */ /* ******************************************** */ /* The purpose of function findMin is to set parameter minValue equal to the minimum of the values in the data array between index positions 0 and numSlotsUsed-1 (inclusive), and also to set parameter minPos equal to the smallest array index between 0 and numSlotsUsed-1 such that data[minPos] is equal to the minimum value. HINTS Look on page 414, on lines 58-65 of the program code, to see an example of how to find the minimum of an array of numbers. The "start_index" for findMin should just be 0. */ void findMin (int data[ ], int & minValue, int & minPos, int numSlotsUsed ) { } /* ******************************************** */ /* FINDMAX */ /* ******************************************** */ /* The purpose of function findMax is to set parameter maxValue equal to the maximum of the values in the data array between index positions 0 and numSlotsUsed-1 (inclusive), and also to set parameter maxPos equal to the smallest array index between 0 and numSlotsUsed-1 such that data[maxPos] is equal to the maximum value. HINTS Look on page 414, on lines 58-65 of the program code, to see an example of how to find the minimum of an array of numbers. The job of finding the maximum is very similar. The "start_index" for findMax should just be 0. */ void findMax (int data[ ], int & maxValue, int & maxPos, int numSlotsUsed ) { } /* ******************************************** */ /* AVERAGE */ /* ******************************************** */ /* The purpose of function average is return the average of the values data[0] through data[numSlotsUsed-1]. The average is the sum of data[0] through data[numSlotsUsed-1], divided by numSlotsUsed. HINTS On page 407 of the text, there is an example (lines 46-52) of function code that computes the average of the values in an array. */ double average (int data[ ], int numSlotsUsed ) { } /* ******************************************** */ /* STNDRDDEV */ /* ******************************************** */ /* The purpose of function stndrdDev is to return the population standard deviation of the values data[0] through data[numSlotsUsed-1]. To see how to calculate the population standard deviation, consider terms of this form: ( data[k] - average ). The parameter called average is assumed to be the average of the values data[0] through data[numSlotsUsed-1]. If we sum the squares of each of the terms shown above, then divide the result by numSlotsUsed, and finally take the square root of the result, that final value is the population standard deviation. See this web page for a formula written out in mathematical notation and then explained one small step at a time: http://www.mathsisfun.com/data/standard-deviation-formulas.html (However of course you must adjust for the fact that N items in a C++ array are numbered starting at 0 and ending with N-1, whereas in the explanation on the web page the authors assumed that there are N items numbered starting with item 1 and ending with item N.) HINTS The code for computing the standard deviation can be a lot like the code for computing the average. One computes the square root of an average of some terms. Each individual term is a "square difference." The difference is the difference between the array value and the average -- in other words you average all terms of the form: (data[k]-average)*(data[k]-average) You average all terms like that, where the index k ranges from 0 to numSlotsUsed-1. */ double stndrdDev (double average, int data[ ], int numSlotsUsed ) { }