25/11/2015
//Author: David Riggleman
See Project:....
//*****************************************************************************
//File Name: main.cpp
//
//Summary: this program reads in a list of numbers from a file, computes, the
// mean, finds the min/max, sorts the list, and the prints to the screen and
// then writes to an output file
//
//Author: David Riggleman
//Created: 13 January 2009
//*****************************************************************************
//needse
//used for external file input and output
using namespace std;
//******************** Input/Output Functions *******************************
int inputFromFile(int list[]);
//precondition: the function reads in a list of numbers from a file which will
// be specified by the user. The first number of the list must be used to
// to indicate the number of items in the list. In additino, the function
// must be passed an empty array
//postcondition: the list of numbers from the input file will be stored in the
// array and the number of items (the array size) will be returned
void outputToStream(double mean, int min, int max, const int list[],
int arraySize, ostream& outputStream);
//precondition: if writing to an external file, outputStream must already have
// been opened
//postcondition: the function ouputs the results to an output stream, either to
// the screen or to an output file
void initializeFileOutput(ofstream& fout);
//postcondition: the function asks the user to name the output file and then
// opens the output file stream.
//******************** Computational Functions ********************************
double computeMean (const int list[], int arraySize);
//postcondition: the funciton calculates the mean given a list of numbers and
// the number of items in the list.
int findExtreme(const int list[], int arraySize, bool isMax);
//precondition: the boolean variable end determines whether the function will
// return either the max or min. If end = 0 (low), the minimum will be returned
// and if end = 1 (high), the maximum will be returned
//postcondition: the function returns the extreme specified by the boolean
// paramater (either a min or max)
//*********************** Sorting Functions **********************************
void sort (int list[], int arraySize);
//postcondition: the items in the array are sorted using a selection sort
void swapValues(int& number1, int& number2);
//postcondition: the function swaps the values of the two integers passed to
// this function
int main(int argc, char *argv[])
{
const int MAX_ARRAY_SIZE = 1000;
int numberedList[MAX_ARRAY_SIZE];
int arraySize = inputFromFile(numberedList);
double mean = computeMean(numberedList, arraySize);
int minimum = findExtreme(numberedList, arraySize, 0);
int maximum = findExtreme(numberedList, arraySize, 1);
sort(numberedList,arraySize);
ofstream fout;
initializeFileOutput(fout);
outputToStream(mean, minimum , maximum, numberedList, arraySize, cout);
outputToStream(mean, minimum , maximum, numberedList, arraySize, fout);
fout.close();
system("PAUSE");
return EXIT_SUCCESS;
}
//******************** Input/Output Functions *******************************
int inputFromFile(int list[])
{
int arraySize = 0;
char inputFile[32];
cout > inputFile;
ifstream fin;
fin.open(inputFile);
if(fin.fail()) //checks that the input file opened successfully
{
cout > list[counter];
}
fin.close();
return arraySize;
}
void outputToStream(double mean,int min,int max,
const int list[], int arraySize, ostream& outputStream)
{
outputStream