1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#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)
{
}
|