//Written by Mark Lair

//October 08, 2007

//Chapter 7

//Assignment 2, Rainfall Statistics

// this program will calculate and display
// the total, average monthly, highest and lowest rainfall
// for 12 months.

#include <iostream>
#include <iomanip>
using namespace std;

//global constants
	const int NUM_MONTHS = 12; //# of months
	const int STRING_SIZE = 10; // max string size

	
// Function prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
int getHighestMonth(double[], int);
int getLowestMonth(double[], int);
void rainSort(char [][STRING_SIZE], double [], int);
void showSorted(double [], char [][STRING_SIZE], int);

int main()
{


	double rainfall[NUM_MONTHS], //holds rainfall amounts

			total,				// holds total rainfall
			average,			// holds average rainfall
			highest,			// holds highest rainfall
			lowest;				// holds lowest rainfall
	int hMonth;					// holds highest rainfall month
	int lMonth;					// holds lowest rainfall month
	char months[NUM_MONTHS][STRING_SIZE] = // holds names of months
	{ "January", "February", "March",
	"April", "May", "June", "July",
	"August", "September", "October",
	"November", "December" };
//	int monthNum[NUM_MONTHS]; // holds month numbers tha can be ordered	
	
	// Get rainfall data
	cout << "Enter the rainfall totals in inches for each month.\n";
	for (int count = 0; count < NUM_MONTHS; count++)
	{
		cout << months[count] << ": ";
		cin >> rainfall[count];

		while (rainfall[count] < 0) 
		{
			cout << "Error - no such thing as a negative rain amount \n";
			cout << "... please enter a positive number! \n";
			cout << months[count] << ": ";
			cin >> rainfall[count];
		}


	}


	
	// Get the total rainfall
	total = sumArray(rainfall, NUM_MONTHS);

	// Calculate the average rainfall
	average = total / NUM_MONTHS;

	//Find highest rainfall amount 
	highest = getHighest(rainfall, NUM_MONTHS);

	//Find lowest rainfall amount 
	lowest = getLowest(rainfall, NUM_MONTHS);

	//Find highest rainfall amount 
	hMonth = getHighestMonth(rainfall, NUM_MONTHS);

	//Find lowest rainfall amount 
	lMonth = getLowestMonth(rainfall, NUM_MONTHS);


	// Display results
	cout << fixed << showpoint << setprecision(2);
	cout << "The total rainfall in inches is: " << total << endl;
	cout << "The average monthly rainfall amount is: " << average << endl;
	cout << "The Highest rainfall amount occurred in " << months[hMonth] << " at " << highest << " inches." << endl;
	cout << "The Lowest rainfall amount occurred in " << months[lMonth] << " at " << lowest << " inches." << endl;

	
	
	
	//(dual) Sort rainfall from highest to lowest
	rainSort(months, rainfall, NUM_MONTHS);




	// Show sorted months and rainfall
	showSorted(rainfall, months, NUM_MONTHS);







	system("pause");
	return 0;
}

	


	// Function to find the total rainfall

	double sumArray(double array[], int size)
	{
		double total = 0; //Accumulator
		
		for (int count = 0; count < size; count++)
			total += array[count];
		return total;
	}


	
	
	// Function to find highest rainfall

	double getHighest(double array[], int size)
	{
		double highest;

		highest = array[0];
		for (int count = 1; count < size; count++)
		{
			if (array[count] > highest)
				highest = array[count];
		}
		return highest;
	}


	// Function to find highest rainfall month

	int getHighestMonth(double array[], int size)
	{
		double highest;
		int month;

		highest = array[0];
		for (int count = 1; count < size; count++)
		{
			if (array[count] > highest)
			{
				highest = array[count];
				month = count;
			}
		}
		return month;
	}



	// Function to find lowest rainfall

	double getLowest(double array[], int size)
	{
		double lowest;

		lowest = array[0];
		for (int count = 1; count < size; count++)
		{
			if (array[count] < lowest)
				lowest = array[count];
		}
		return lowest;
	}


	// Function to find lowest rainfall month

	int getLowestMonth(double array[], int size)
	{
		double lowest;
		int month;

		lowest = array[0];
		for (int count = 1; count < size; count++)
		{
			if (array[count] < lowest)
			{
				lowest = array[count];
				month = count;
			}
		}
		return month;
	}



	// Function to sort the rainfall from highest to lowest
	// while also keeping the months parallel

	void rainSort(char mName[][STRING_SIZE], double rain[], int size)
	{
		int startScan, maxIndex;
		char tempName[NUM_MONTHS][STRING_SIZE];
		double maxValue;

		for (startScan = 0; startScan < (size-1); startScan++)
		{
			maxIndex = startScan;
			maxValue = rain[startScan];
			for (int i = 0; i < 10; i++) 
			tempName[startScan][i] = mName[startScan][i];
			
			for(int index = startScan + 1; index < size; index++)
			{
				if (rain[index] > maxValue)
				{
					maxValue = rain[index];
					for (int i = 0; i < 10; i++) 
					tempName[index][i] = mName[index][i];
					maxIndex = index;
				}
			}
			
			rain[maxIndex] = rain[startScan];
			for (i = 0; i < 10; i++) 
			mName[maxIndex][i] = mName[startScan][i];
			rain[startScan] = maxValue;
			for (i = 0; i < 10; i++) 
			mName[startScan][i] = tempName[startScan][i];
		}
	}

	//display sorted months and rainfall

	void showSorted(double rain[], char mName[][STRING_SIZE], int size)
	{
		cout << " " << endl;
		cout << "Here are the months and rainfalls in order from highest to lowest:" << endl;
		cout << " " << endl;
		for(int index = 0; index < size; index++)
		{

			cout << mName[index] << endl;
			cout << rain[index] << endl;
			cout << " " << endl;
			
		}
	}
