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 111 112 113 114 115 116 117 118 119 120 121 122 123
|
const int NUMBER_OF_MONTHS = 12;
//get the Contestant's name and scores and calculates the total
void getRainfall(double rainfall[]);
//calculates the average score using the total from previous function
double calculateTotal(double rainfall[]);
//calculates the average score using the total from previous function
double calculateAverage(double total);
void findHighAndLow(double& highestmonth, double& lowestmonth, double& highestamount, double& lowestamount, double rainfall[], int MONTHS);
//displays name, all scores entered, and average
void displayInformation(double rainfall[], double total, double average, double highestamount, double highestmonth, double lowestamount, double lowestmonth);
int main(void)
{
//Variables for rainfall over a period of months and average rainfall over a period of years.
int MONTHS = 12;
double rainfall[NUMBER_OF_MONTHS] = { 0 };
double average = 0, total = 0, highestamount = 0, lowestamount = 0, highestmonth = 0, lowestmonth = 0;
string dummy;
char repeat;
//Prompt and read the number of period of months and years and calculates the total rainfall for the year
do
{
average = 0, total = 0, highestamount = 0, lowestamount = 0, highestmonth = 0, lowestmonth = 0;
getRainfall(rainfall);
total = calculateTotal(rainfall);
average = calculateAverage(total);
findHighAndLow(highestmonth, lowestmonth, highestamount, lowestamount, rainfall, MONTHS);
displayInformation(rainfall, total, average, highestamount, highestmonth, lowestamount, lowestmonth);
cout << "\nWould you like to run the program again? Y or N ";
cin >> repeat;
getline(cin, dummy);
} while (repeat == 'y' || repeat == 'Y');
return 0;
}
//Prompt and display the rainfall
void getRainfall(double rainfall[])
{
for (int i = 0; i < NUMBER_OF_MONTHS; i++)
{
cout << "Enter the rainfall for month #" << i + 1 << ": ";
cin >> rainfall[i];
while (rainfall <= 0)
{
cout << "**** ERROR - AMOUNT CAN'T BE NEGATIVE - PLEASE TRY AGAIN ***" << endl;
cout << "Enter the rainfall for month # " << i << "? ";
cin >> rainfall[i];
}
}
}
//Calculates the total rainfall
double calculateTotal(double rainfall[])
{
double total = 0;
for (int i = 0; i < NUMBER_OF_MONTHS; i++)
{
total = total + rainfall[i];
}
return total;
}
//Calculates the average rainfall for the year
double calculateAverage(double total)
{
double average = 0;
average = total / NUMBER_OF_MONTHS;
return average;
}
//Determine the largest amount and smallest amount of rainfall recorded for one month
void findHighAndLow(double& highestmonth, double& lowestmonth, double& highestamount, double& lowestamount, double rainfall[], int MONTHS)
{
highestamount = rainfall[0];
lowestamount = rainfall[0];
for (int count = 0; count < NUMBER_OF_MONTHS; count++)
{
if (rainfall[count] > highestamount)
{
highestamount = rainfall[count];
highestmonth = count + 1;
}
if (rainfall[count] < lowestamount)
{
lowestamount = rainfall[count];
lowestmonth = count + 1;
}
}
}
//Displays the original rainfall amounts, total and average amount of rainfall for the year, and the amounts and highest and lowest amount of rainfall
void displayInformation(double rainfall[], double total, double average, double highestamount, double highestmonth, double lowestamount, double lowestmonth)
{
cout << "\nThe rainfalls for the following months were:" << endl;
for (int i = 0; i < NUMBER_OF_MONTHS ; i++)
{
cout << " Months " << i + 1 << ": " << rainfall[i] << endl;
}
cout << endl;
cout << "The total rainfall for the year was " << setprecision(2) << fixed << showpoint << total << " inches." << endl;
cout << "The average rainfall for the year was " << setprecision(2) << fixed << showpoint << average << " inches." << endl;
cout << "\nThe month(s) that recorded the highest amount of rain of " << setprecision(2) << fixed << showpoint << highestamount << " inch(es) was (were):" << endl;
cout << "Months#: " << setprecision(0) << fixed << showpoint << highestmonth << endl;
cout << "\nThe month(s) that recorded the lowest amount of rain of " << setprecision(2) << fixed << showpoint << lowestamount << " inch(es) was (were): " << endl;
cout << "Months#: " << setprecision(0) << fixed << showpoint << lowestmonth << endl;
}
|