fork download
// Kurt Feiereisel				    CSC5				    Chapter 7, p.444, #2
/*******************************************************************************
 *
 * Calculate Rainfall Statistics
 * _____________________________________________________________________________
 * This program accepts input as the amount of rainfall each month for a year.
 * The program will then calculate and report the total rainfall for the year,
 * average rainfall for the year, the month with the highest rainfall, and the 
 * month with the lowest rainfall.
 * _____________________________________________________________________________
 * FORMULA:
 *		averageRain = tr / months
 * INPUT:
 *  	rain[]			: Rainfall per month of the year
 *		
 * OUTPUT:
 *		totalRain		: Total rainfall for the year
 *		averageRain		: Average Rainfall for the year
 *		highest			: Most amount of rainfall in a month
 *		lowest			: Least amount of rainfall in a month
 *		mostRain		: Month with the most rain
 *		leastRain		: Month with the least rain
 * 
 * ****************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Function Prototypes
void rainfall(float rain[], int MONTHS);
float total(float rain[], int MONTHS);
float average(float totalRain, int MONTHS);
float highestRain(float rain[], int MONTHS);
float lowestRain(float rain[], int MONTHS);
void display(float totalRain, float averageRain, float highest, float lowest,
		string mostRain, string leastRain);
string highestMonth(float rain[], int MONTHS, float highest);
string lowestMonth (float rain[], int MONTHS, float lowest);

int main()
{
	// Initialize Constant
	const int MONTHS = 12;
	
	// Declare Array
	float rain[MONTHS];
	
	// Declare Variables
	float averageRain;			// Output: Average amount of rainfall in a year
	float totalRain;			// Output: Total rainfall for the year
	float highest;				// Output: Most amount of rain in a month
	float lowest;				// Output: Least amount of rain in a month
	string mostRain;			// Output: Month with the most rain
	string leastRain;			// Output: Month with the least rain
	
	// Call Functions
	rainfall(rain, MONTHS);
	totalRain = total(rain, MONTHS);
	averageRain = average(totalRain, MONTHS);
	highest = highestRain(rain, MONTHS);
	lowest = lowestRain(rain, MONTHS);
	mostRain = highestMonth(rain, MONTHS, highest);
	leastRain = lowestMonth(rain, MONTHS, lowest);
	display(totalRain, averageRain, highest, lowest, mostRain, leastRain);

	return 0;
}

/*******************************************************************************
 * Definition of function rainfall 
 * This function accepts input as the amount of rain per month of the year
 ******************************************************************************/
void rainfall(float r[], int months)
{
	// Input the Rainfall for the month
	cout << "Please enter the rainfall for each month"
		 << " this past year." << endl;
	for(int index = 0; index < months; index++)
	{
		cin >> r[index];
		
		// Input Verification
		while (r[index] < 0)
		{
			cout << "Please enter a positive number." << endl;
			cin >> r[index];
		}
	}
}

/*******************************************************************************
 * Definition of function total 
 * This function accumulates the total rainfall for the year
 ******************************************************************************/
float total(float r[], int months)
{
	// Initialize Accumulator
	float t = 0;					// Accumulator: Sums up all the rainfall
									// per month of the year
	// Implement for loop with accumulator
	for(int index = 0; index < months; index++)
		t += r[index];
	return t;
}

/*******************************************************************************
 * Definition of function average 
 * This function calculates the average amount of rainfall for the year
 ******************************************************************************/
float average(float tr, int months)
{
	// Initialize Variable
	float avg = 0;
	// Calculate the average amount of rainfall for the year
	avg = tr / months;
	return avg;
}

/*******************************************************************************
 * Definition of function highestRain 
 * This function determines the highest amount of rainfall during a month for 
 * the year.
 ******************************************************************************/
float highestRain(float r[], int months)
{
	// Initialize Variable
	float highest = r[0];
	
	// For loop to find the highest value
	for(int index = 0; index < months; index++)
	{
		if (r[index] > highest)
			highest = r[index];
	}
	return highest;
}

/*******************************************************************************
 * Definition of function lowestRain 
 * This function determines the lowest amount of rainfall in a month for the
 * year
 ******************************************************************************/
float lowestRain(float r[], int months)
{
	// Initialize Variable
	float lowest = r[0];
	
	// For loop to determine lowest value
	for(int index = 0; index < months; index++)
	{
		if(r[index] < lowest)
			lowest = r[index];
	}
	return lowest;
}

/*******************************************************************************
 * Definition of function highestMonth 
 * This function determines which month has the highest amount of rainfall
 ******************************************************************************/
string highestMonth(float r[], int months, float highest)
{
	// Declare Variable
	string month;
	
	// Determine the element that is equal to the highest amount of rainfall
	for (int index = 0; index < months; index++)
	{
		if(highest == r[index])
		{
			// Inialize month with correspoinding subscript
			switch(index)
			{
				case 0: month = "January";
					break;
				case 1: month = "February";
					break;
				case 2: month ="March";
					break;
				case 3: month = "April";
					break;
				case 4: month = "May";
					break;
				case 5: month = "June";
					break;
				case 6: month = "July";
					break;
				case 7: month = "August";
					break;
				case 8: month = "September";
					break;
				case 9: month = "October";
					break;
				case 10: month = "November";
					break;
				case 11: month = "December";
					break;
				default: month = "Not a valid month";
					break;
			}
		}
	}
	return month;
}

/*******************************************************************************
 * Definition of function lowestMonth 
 * This function determines which month has the lowest amount of rainfall
 ******************************************************************************/
string lowestMonth(float r[], int months, float lowest)
{
	// Initialize Variable
	string month;
	for (int index = 0; index < months; index++)
	{
		// Determine the element that is equal to the highest value
		if(lowest == r[index])
		{
			
			// Initalize month with correspoinding subscript
			switch(index)
			{
				case 0: month = "January";
					break;
				case 1: month = "February";
					break;
				case 2: month ="March";
					break;
				case 3: month = "April";
					break;
				case 4: month = "May";
					break;
				case 5: month = "June";
					break;
				case 6: month = "July";
					break;
				case 7: month = "August";
					break;
				case 8: month = "September";
					break;
				case 9: month = "October";
					break;
				case 10: month = "November";
				break;
				case 11: month = "December";
					break;
				default: month = "Not a valid month";
			}
		}
	}
	return month;
}
/*******************************************************************************
 * Definition of function display
 * This function displays the total rainfall, average rainfall, lowest rainfall
 * highest rainfall, and the month with the lowest and highest rainfall amounts
 ******************************************************************************/
void display(float t, float a, float h, float l, string m, string low)
{
	cout << fixed << setprecision(2);
	// Display Outputs
	cout << "The total rainfall this past year is " << t << " inches.\n";
	cout << "The average rainfall this past year is " << a << " inches.\n";
	cout << low << " had the least amount of rainfall with " << l
		 << " inches.\n";
	cout << m << " had the most amount of rainfall with " << h << " inches.\n";
}

Success #stdin #stdout 0.01s 5272KB
stdin
1
2
3
4
5
6
7
8
9
10
11
12
stdout
Please enter the rainfall for each month this past year.
The total rainfall this past year is 78.00 inches.
The average rainfall this past year is 6.50 inches.
January had the least amount of rainfall with 1.00 inches.
December had the most amount of rainfall with 12.00 inches.