/* ********************************************
		Program: lab11tp.cpp
		
		Tonya M. Payne
		COSC 1435 :: Section 3
		
		Version: 1.0
		Date:		Original 7 April 2010
		Purpose:	To make arrays that do interesting things
					like print, add, average, etc.
		
		Input: Keyboard
		Output: To screen


*///////////////////////////////////////////////


#include<iostream>	//needed for compiler
#include<fstream>		//needed for reading in files
#include<iomanip>		//for formatting

using namespace std; 

//function prototypes
void printArray(int iNumbers[], int iMax);
int addArray(int iNumber[], int iMax);
double meanArray(int iNumber[], int iMax);
double medianArray(int iNumber[], int iMax);
int hVArray(int[], int);
int	lVArray(int[], int);

void sort(int [], const int iSize);


//main part of the progam
int main()
{
	ifstream fsIn;
	int 	iValue, 
			iCount = 0;
			
	const int MAX = 500;
	int 	iArray[MAX],
			iaCopy[MAX];
	
	fsIn.open( "integers.txt" );
	if ( !fsIn )
	{
		cout << "ERROR, integers.txt did not open properly\n";
		return -1;
	}

	fsIn >> iValue;
	while ( !fsIn.eof() && iCount < MAX )
	{
		iArray[iCount] = iValue;
		fsIn >> iValue;
		iCount++;
	}
	
	fsIn.close();
	
	cout << "Here is the array, it has " << iCount << " members." << endl;
	printArray(iArray, iCount);
	
	//copy the array
	for ( int j = 0; j < MAX; j++ )
		{
			iaCopy[j] = iArray[j];
		}
	sort(iaCopy, iCount);
		
	//the maths print from here
	cout << endl << "The total summation of this array is: ";
	cout << addArray(iArray, iCount) << endl;
	
	cout << endl << "The mean (average) of this array is: ";
	cout << meanArray(iArray, iCount) << endl;
	
	cout << "The median of this array is: " << medianArray(iaCopy, iCount);
	cout << endl;
	
	cout << "The high value of this array is: " ;
	cout << iArray[hVArray(iArray, iCount)];
	cout << endl;
	
	cout << "The lowest value in this array is: " ;
	cout << iArray[lVArray(iArray, iCount)];
	cout << endl;
	
	cout << endl;
	return 0; //end of program
}

//*****************************************************************************
//	function for printing the array
//	
//	variables in the function: 	iNumbers: each number in the array
//								iMax: the maximum size of the array
//								i: a counter
//
//*****************************************************************************
void printArray(int iNumbers[], int iMax)
{
	for ( int i = 0; i < iMax; i++ )
	{
		cout << setw(4) << i << ": " << iNumbers[i] << endl;
	}
}

//*****************************************************************************
//	function for adding the array
//	
//	variables in the function: 	iNumbers: each number in the array
//								iMax: the maximum size of the array
//								i: a counter
//
//*****************************************************************************

int addArray(int iNumber[], int iMax)
{
	int 	iTemp = 0,
			iAdd = 0;
	for ( int i = 0; i < iMax; i++ )
	{	
		iTemp = iTemp + iNumber[i];
		iAdd = iTemp;
	}
	return iAdd;
}


//*****************************************************************************
//	function for finding the mean (average) of the array
//	
//	variables in the function: 	iNumbers: each number in the array
//								iMax: the maximum size of the array
//								i: a counter
//
//*****************************************************************************

double meanArray(int iNumber[], int iMax)
{
	int 	iTemp = 0,
			iAdd = 0;
	double	dMean = 0;
	
	for ( int i = 0; i < iMax; i++ )
	{	
		iTemp = iTemp + iNumber[i];
		iAdd = iTemp;
	}
	
	dMean = static_cast<double>( iAdd ) / iMax ;
	
	return dMean;
}

//*****************************************************************************
//	function for finding the median of the array
//	
//	variables in the function: 	iNumbers: each number in the array
//								iMax: the maximum size of the array
//								i: a counter
//
//*****************************************************************************

double medianArray(int iNumber[], int iMax)
{
	int 	iHalf = 0;
	double	dMedian = 0;
	
	iHalf = iMax / 2;
	if ( iMax % 2 != 0 )
	{
		dMedian = iNumber[iHalf];
	}
	else
	{
		dMedian = (static_cast<double>(iNumber[iHalf]) + iNumber[iHalf-1] ) / 2;
	}
	return dMedian;
}

/****************************************************************************
*	Attribution:  	The selection sorts was taken from "Starting Out with C++: 
*						From Control Structures through
*						Objects, 5th Edition", by Tony Gaddis.  I modified the
*						variable names to camel back style.  It is otherwise
*						unchanged.
****************************************************************************/

void sort(int ia[], int iSize)
{
   static int iStartScan, iMinIndex;
   int iMinValue;
   for (iStartScan = 0; iStartScan < (iSize -1); iStartScan++)
   {
      iMinIndex = iStartScan;
      iMinValue = ia[iStartScan];
      for (int i = iStartScan + 1; i < iSize; i++)
      {
         if (ia[i] < iMinValue)
         {
            iMinValue = ia[i];
            iMinIndex = i;
         }
      }
      ia[iMinIndex] = ia[iStartScan];
      ia[iStartScan] = iMinValue;
   }
}

//*****************************************************************************
//	function for finding the highest value of the array
//	
//	variables in the function: 	iNumbers: each number in the array
//								iMax: the maximum size of the array
//								i: a counter
//
//*****************************************************************************

int hVArray(int iNumber[], int iMax)
{
	int highVar = 0,
		heldHVar = iNumber[0];
		
		for ( int k = 0; k < iMax; k++ )
			{
				if ( iNumber[k] > heldHVar )
				{
					heldHVar = iNumber[k];
					highVar = k;
				}
			}
	return highVar;
}

//*****************************************************************************
//	function for finding the lowest value of the array
//	
//	variables in the function: 	iNumbers: each number in the array
//								iMax: the maximum size of the array
//								i: a counter
//
//*****************************************************************************

int lVArray(int iNumber[], int iMax)
{
	int lowVar = 0,
		heldLVar = iNumber[0];
		
		for ( int m = 0; m < iMax; m++ )
			{
				if ( iNumber[m] < heldLVar )
				{
					heldLVar = iNumber[m];
					lowVar = m;
				}
			}
	return lowVar;
}

