//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;

// Function prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
int getHighestMonth(double[], int);
int getLowestMonth(double[], int);

int main()
{
	const int NUM_MONTHS = 12; //# of months
	const int STRING_SIZE = 10; // max string size
	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" };
	
	
	
	// 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;


	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;
	}

