1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*
Josh Krynock CodeLab 70002
Rainfall Statistics
Write a program that lets the user enter the total rainfall for each of 12 months
(starting with January) into an array of doubles .
The program should calculate and display (in this order):
the total rainfall for the year,
the average monthly rainfall,
and the months with the highest and lowest amounts.
Months should be expressed as English names for months in the Gregorian calendar,
i.e.: January, February, March, April, May, June, July,
August, September, October, November, December.
*/
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void getRainfall(double[], int);
double getTotalRF(const double[], int);
int getLowRF(const double[], int);
int getHighRF(const double[], int);
//Array names
const int NUM_MONTHS = 12;
double rainfall[NUM_MONTHS];
//Month array to hold names of months
string month[NUM_MONTHS] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
int main()
{
//Variables to hold total, high/low score and average.
double total, average;
int low, high;
getRainfall(rainfall, NUM_MONTHS);
total = getTotalRF(rainfall, NUM_MONTHS);
low = getLowRF(rainfall, NUM_MONTHS);
high = getHighRF(rainfall, NUM_MONTHS);
cout << "Total rainfall: " << total << endl;
cout << "Average rainfall: " << (total / NUM_MONTHS) << endl;
cout << "Least rainfall in: " << month[low] << endl;
cout << "Most rainfall in: " << month[high] << endl;
return 0;
}
void getRainfall (double rainfall[], int size)
{
int index; //Loop counter
for (index = 0; index < size; index ++)
{
cout << "Enter rainfall for " << month[index] << ": ";
cin >> rainfall[index];
}
}
double getTotalRF(const double amount[], int size)
{
double total = 0; //accumulator
for (int index = 0; index < size; index++)
total += amount[index];
return total;
}
int getLowRF(const double amount[], int size)
{
double low; //Variable to hold lowest value
int lowMonth; //Variable to return month element location.
low = amount[0];
lowMonth = 0; //Set low value to intial rainfall value, January which is 0
for (int index = 1; index < size; index++)
{
if (amount[index] < low)
{
low = amount[index];
lowMonth = index;
}
}
return lowMonth;
}
int getHighRF(const double amount[], int size)
{
double high; //Variable to hold highest value
int highMonth; //Variable to return month element location.
high = amount[0];
highMonth = 0;
for (int index = 1; index < size; index++)
{
if (amount[index] > high)
{
high = amount[index];
highMonth = index;
}
}
return highMonth;
}
|