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

C++ LANGUAGE! Question: Average Rainfall Write a program that uses nested loops

ID: 3793924 • Letter: C

Question

C++ LANGUAGE!

Question: Average Rainfall

Write a program that uses nested loops to collect data and calculate the average monthly rainfall last year. The program uses a loop to collect the rainfall for the 12 months. The program should then calculates the average monthly rainfall and display the total inches of rainfall, and the average rainfall per month for last year.

Input validation: You need to use a loop to validate the number of inches of rainfall for each month. Do not accept negative number for the monthly rainfall.

Explanation / Answer

#include <iostream>
using namespace std;

int main()
{

   double rainfall[12], avg_Rainfall;
   double totalRainfall = 0;      //variable to hold the total rainfall
   int i;
   cout << "Enter rainfall for months in inches: " << endl;
   for(i=0;i<12;i++)
   {
   cin >> rainfall[i];
   if(rainfall[i] < 0) //validation
   {
   cout<<" Rainfall for a month can't be negative. Please retenter:";
   cin >> rainfall[i];
   }

   totalRainfall = totalRainfall + rainfall[i];


   }

   avg_Rainfall = totalRainfall / 12; //calculate the average

   cout<<" Total inches of rainfall : "<<totalRainfall<<" inches";
   cout<<" Average rainfall : "<<avg_Rainfall<<" inches";


   return 0;
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote