#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function prototypes
void showIntro();
void getrainfall(double [], string [], int );
double getTotal(double [], int );
double getHighest(double [], string [], int );
double getLowest(double [], string [], int );
//Global variable
const int SIZE = 12; // Array size
// Position of search values: highest and lowest.
int hPosition = -1,
lPosition = -1;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// main function ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
int main()
{
double rainfall[SIZE]; // Array to hold rainfall values
double total, // Total yearly rainfall
highestRainfall, // Highest rainfall
lowestRainfall, // Lowest rainfall
average; // Average monthly rainfall value
string monthName[SIZE] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
char repeat; // to hold Y or N input.
// Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
do
{
// Display introduction.
showIntro();
// Get the monthly rainfall values from the user.
getRainfall(rainfall, monthName, SIZE);
// Get the total of the rainfall values.
total = getTotal(rainfall, SIZE);
// Get the highest rainfall values.
highestRainfall = getHighest(rainfall, monthName, SIZE);
// Get the lowest rainfall values.
lowestRainfall = getLowest(rainfall, monthName, SIZE);
// Calculate the average. Divide by 12.
average = total / SIZE;
// Display figures.
cout << " the total rainfall for the year is : " << total << " \n";
cout << " the monthly average rainfall is : " << average << " \n";
cout<< "The highest rainfall is :" << highestRainfall << "inches, recorded in " << monthName[hPosition] <<endl;
cout<< "The lowest rainfall is :" << lowestRainfall << "inches, recorded in " << monthName[lPosition] <<endl;
// Does the user want to run this program again?
cout<<"Do you want to repeat this program? (Y/N)";
cin >> repeat;
cout<<""<<endl;
}while (repeat=='Y'||repeat=='y');
system("pause");
return 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The getRainfall function recieves two arrays double and string ^
// and their size as arguments. It prompts the user to enter ^
// rainfall value, which are stored in the double array. ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
void getrainfall(double rainfallVal[], string monthName[], int SIZE)
{
// Get each rainfall value.
for(int index = 0; index < SIZE ; index++)
{
cout << "Enter rainfall value. \n";
cout << monthName[index] << ": ";
cin >> rainfallVal[index];
while (rainfallVal[index] < 0)
{
cout<<" You enter a wrong value\n";
cout << "Please enter a positive number.\n";
cout<< monthName[index] << ":" <<" ";
cin >> rainfallVal[index];
}
}
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The getTotal function accepts a double array ^
// and its size as arguments. The sum of the array's ^
// elements is returned as an double. ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
double getTotal(double value[], int SIZE)
{
double totalRain = 0; // Accumulator
// Add each rainfall value to total.
for (int count = 0; count < SIZE; count++)
totalRain += value[count];
// Return the total.
return totalRain;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The getHighest function accepts a double array and ^
// its size as arguments. The Highest rainfall value ^
// in the array is returned as an double. ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
double getHighest(double rainfallVal[], string monthName[], int SIZE)
{
double highest; // To hold the highest value.
//string month; // to hold the month with the highest rainfall value.
// Get the first array's first element.
highest = rainfallVal[0];
//fine the highest array's value and assign it to highest.
for (int count = 1; count < SIZE; count++)
{
if (rainfallVal[count] > highest)
{
highest = rainfallVal[count];
hPosition = count;
}
}
cout<< "The highest rainfall is :" << highest << "inches, recorded in " << monthName[hPosition] <<endl;
// Return the highest rainfall's value.
return highest;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The getLowest function accepts a double array and ^
// its size as arguments. The lowest rainfall value ^
// in the array is returned as an double. ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
double getLowest(double rainfallVal[], string monthName[], int SIZE)
{
double lowest; // To hold the lowest value
// Get the first array's first element.
lowest = rainfallVal[0];
//fine the the lowest array,s value and assign it to lowest.
for (int count = 1; count < SIZE; count++)
{
if (rainfallVal[count] < lowest)
{
lowest = rainfallVal[count];
lPosition = count;
}
cout<< "The lowest rainfall is :" << lowest << "inches, recorded in " << monthName[lPosition] <<endl;
}
// Return the lowest rainfall's value.
return lowest;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The function showIntro displays the introduction ^
//message of the program to instruct the user. ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
void showIntro()
{
cout << "\t############################################################\n";
cout << "\t# This program prompt you to enter the rainfall amount #\n";
cout << "\t# for each of 12 months so it can calculate and display #\n";
cout << "\t# the total rainfall for the year, the average monthly #\n";
cout << "\t# rainfall, and determine the month with the highest #\n";
cout << "\t# and lowest rainfall amounts. #\n";
cout << "\t############################################################\n";