(Latest Revision: Sat Nov 23 15:10 PST 2013 )
/* ******************************************** */
/* GETDATA */
/* ******************************************** */
/*
The purpose of function getData is to
+ read from the input file whose name is stored in: fname,
+ read the first number in the file to determine how many
data items are in the rest of the file,
+ copy the data items 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 data item in the file, if one
exists, into data[0], copies the second data item into data[1], and so
on.)
Function getData handles the following exceptional situations:
+ input file cannot be opened,
+ input file has no data items, and
+ input file contains more data items than the value of arraySize.
In those situations, function getData prints an error message and
terminates execution of the program (It calls the exit() function.).
HINTS
What code do we need to implement function getData?
Declare an input stream. There's a filename stored in the char array
fname. Open the file, using the filename as the argument to the call
of the ifstream open function. (in the code, include an error-check
for failure to open the file) (See example in 01_read_ints.cpp.html.)
Declare an int variable to use to store the first integer in the
input file - the number that tells you how many data items are in
the file. Give the variable a good name - a name that helps you
remember the purpose for which your program uses the variable.
(I'll just call it "the_variable" below, which is NOT a good name.)
Suppose that the name of the input file stream is f_in. Then a statement
like
f_in >> the_variable ;
will input one value from the file, and copy it into the_variable.
If that value is in the proper range (between 1 and arraySize) then
the code for getData can just copy the value of the_variable into
numSlotsUsed, execute a for-loop that reads the data from the
file into the array, and close the file. On page 386 of the text,
in Display 7.4, on lines 6 and 7 under "Function Definition," there
is an example of such a for-loop, except that it reads from cin.
You can modify that example to create a for-loop that reads from
an ifstream.
If the value of the_variable is more than the arraySize, then your
code should write an appropriate error message, close the file, and
execute an exit(1) call to abort the execution of the program.
Similarly, if the_variable is equal to zero, your code should write
a different error message, close the file and exit.
*/
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 )
{
}