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:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.