SOURCE FILE: statsShell.cpp
/*
The purpose of this program is to work with a file containing (only) 1 or
more integers, interspersed with white space.
The first integer in the file is data size -- the number of additional
integers in the file.
We assume the integers are suitable to be represented using the int data
type.
The name of the input file is the value of the global constant: fileName.
There is another global constant int called: sizeLimit.
If the input file cannot be opened, or if data size is 0, or if data size is
greater than sizeLimit, then the output of the program is an error message.
The program inputs data size. If it is more than zero but not more than the
value of sizeLimit, then the program copies all the integers in the file that
come after data size into an array. Then the program outputs:
* the contents of the array,
* the minimum value found in the array,
along with the index of the first array element equal to the minimum,
* the maximum value found in the array,
along with the index of the first array element equal to the maximum,
* the average (mean) of the numbers in the array, expressed as a
double, and
* the population standard deviation of the numbers in the array, also
expressed as a double.
See the file terminalSession.html for an example illustrating the way that
the output must be formatted.
One may check one's values for averages and population standard deviations
here:
http://www.mathsisfun.com/data/standard-deviation-calculator.html
*/
#include <iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std ;
/*
Some global constants. With this set up, to make the program work
with a different array size or filename, all we have to do is change
the value of the constant in the declaration below, and recompile --
no need to search through the rest of the program to change anything
there.
*/
const int sizeLimit = 100 ;
const char fileName[ ] = "numbers.txt" ;
void getData (int data[ ], const char fname[ ],
int arraySize, int & numSlotsUsed);
void showData (int data[ ], int numSlotsUsed) ;
double average (int data[ ], int numSlotsUsed ) ;
double stndrdDev (double average, int data[ ], int numSlotsUsed ) ;
void findMin (int data[ ], int & minValue, int & minPos, int numSlotsUsed ) ;
void findMax (int data[ ], int & maxValue, int & maxPos, int numSlotsUsed ) ;
void showResults (int min, int minIndex, int max, int maxIndex,
double average, double stdDev) ;
int main (void)
{
int theData[sizeLimit], dataSize, min, max, minIndex, maxIndex ;
double theAve, theStdDev ;
getData(theData, fileName, sizeLimit, dataSize) ;
showData (theData, dataSize) ;
findMin (theData, min, minIndex, dataSize) ;
findMax (theData, max, maxIndex, dataSize) ;
theAve = average(theData, dataSize) ;
theStdDev = stndrdDev(theAve, theData, dataSize) ;
showResults (min, minIndex, max, maxIndex,theAve, theStdDev) ;
return 0;
}
/* ******************************************** */
/* 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.).
*/
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.
*/
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.
*/
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.
*/
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.
*/
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.)
*/
double stndrdDev (double average, int data[ ], int numSlotsUsed )
{
}
/* ******************************************** */
/* SHOWRESULTS */
/* ******************************************** */
/*
The purpose of function showResults is to print to the screen
the values of min, minIndex, max, maxIndex, average, and
stdDev. These are values calculated, respectively, with
functions findMin, findMax, average, and stndrdDev. The double
quantities average and stdDev are to be printed in fixed-point
notation, showing the decimal point, and with exactly five
digits displayed after the decimal point.
See the file terminalSession.html for examples illustrating the
remaining rules for formatting the output of function
showResults.
*/
void showResults (int min, int minIndex, int max, int maxIndex,
double average, double stdDev)
{
}
/* ******************************************** */
/* END */
/* ******************************************** */