Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) Rainfall Statistics Write a program that lets the user enter the total rai

ID: 3805034 • Letter: #

Question

(C++)

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.


Input Validation: Do not accept negative numbers for monthly rainfall figures. When a negative value is entered, the program  outputs "invalid data (negative rainfall) -- retry" and attempts to reread the value .

NOTE: Decimal values should be displayed using default precision, i.e. do not specify precision.

Explanation / Answer

#include <iostream>
using namespace std;

int main() {
   int i,maxIndex=0,minIndex=0;
   char months[12][12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
   double rainfalls[12],maxRainfall=-9999,minRainfall = 9999,total=0; // maxRainFall is set to an arbitrarily low number. minRainFall is set to an arbitrarily high number.
   for(i=0;i<12;i++){
       cout << "Enter the rainfall for month " << months[i] << ": ";
       cin >> rainfalls[i];
       if(rainfalls[i] < 0){
           cout << "invalid data (negative rainfall) -- retry";
           return -1;
       }
   }
   for(i=0;i<12;i++){
       total += rainfalls[i];
       if(maxRainfall < rainfalls[i]){
           maxRainfall = rainfalls[i];
           maxIndex = i;
       }
       if(minRainfall > rainfalls[i]){
           minRainfall = rainfalls[i];
           minIndex = i;
       }
   }
   cout << "Total Rainfall for the year = " << total << ". ";
   cout << "Average Monthly Rainfall for the year = " << (total/12) << ". ";
   cout << "Maximum Rainfall = " << rainfalls[maxIndex] << ", in the month of " << months[maxIndex] << ". ";
   cout << "Minimum Rainfall = " << rainfalls[minIndex] << ", in the month of " << months[minIndex] << ". ";
   return 0;
}