Question

In: Computer Science

Write a C++ program that uses nested loops to collect data and calculate the average rainfall...

Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.

After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

Add additional loops to validate the input: Do not accept a number less than 1 for the number of years. Continue to prompt the user for a number until a valid number is entered. Do not accept negative numbers for the monthly rainfall. Continue to prompt the user for a number until a valid number is entered.

Test Run

This program will calculate average rainfall over a

period of years. How many years do you wish to average? 0

Years must be at least one. Please re-enter: -2

Years must be at least one. Please re-enter: 2

Year 1

Number of inches of rain for month 1? 1.555

Number of inches of rain for month 2? 3.5

Number of inches of rain for month 3? 2

Number of inches of rain for month 4? 4

Number of inches of rain for month 5? 2

Number of inches of rain for month 6? 4

Number of inches of rain for month 7? 2

Number of inches of rain for month 8? 4

Number of inches of rain for month 9? 2

Number of inches of rain for month 10? 4

Number of inches of rain for month 11? 2

Number of inches of rain for month 12? 4

The rainfall for year 1 is 35.055 and the average rainfall for the year is 2.921

Year 2

Number of inches of rain for month 1? -2

Rainfall must be zero or greater.

Number of inches of rain for month 1? 3

Number of inches of rain for month 2? 4

Number of inches of rain for month 3? 3

Number of inches of rain for month 4? 4

Number of inches of rain for month 5? 3

Number of inches of rain for month 6? 4

Number of inches of rain for month 7? 3

Number of inches of rain for month 8? 4

Number of inches of rain for month 9? 3

Number of inches of rain for month 10? 4

Number of inches of rain for month 11? 2.665

Number of inches of rain for month 12? 3.3

The rainfall for year 2 is 40.965 and the average rainfall for the year is 3.414

Over a period of 24 months, 76.020 inches of rain fell.

Average monthly rainfall for the period is 3.168 inches.

Solutions

Expert Solution

Code:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
   int n,i,j;
   double totalSum=0,sum,rain;/*Declaring the variables*/
   cout<<"The program will calculate avaerage rainfall over a period of years."<<endl;
   cout<<"How many years do you wish to average?";
   cin>>n;/*Reading years form the user*/
   while(n<=0)
   {/*This loop iterates untill user enter a number which is greater than or equals 1*/
       cout<<"Year must be at least one.Please re-enter:";
       cin>>n;
   }
   for(i=1;i<=n;i++)
   {/*This loop iterates for 1 to n*/
       sum=0;/*Making sum as 0 for every year*/
       cout<<"Year "<<i<<endl;
       for(j=1;j<=12;j++)
       {
           cout<<"Number of inches of rain for month "<<j<<"?";
           cin>>rain;/*Reading rain fall for every month*/
           while(rain<0)/*This loop iteraes untill user enters rain fall 0 or greater*/
           {
               cout<<"Rainfall must be zero or greater."<<endl;
               cout<<"Number of inches of rain for month "<<j<<"?";
               cin>>rain;
           }
           sum+=rain;/*Adding the rain fall for every month*/
       }
       cout<<fixed<<setprecision(3)<<"The rainfall for year "<<i<<" is "<<sum<<" and the average rainfall for the year is "<<sum/12.0<<endl;
       /*Printing the result of one year*/
       totalSum+=sum;/*Adding the rain fall the total period rain fall*/          
   }
   cout<<"Over a period of "<<n*12<<" months,"<<totalSum<<" inches of rain fell."<<endl;
   cout<<fixed<<setprecision(3)<<"Average Monthly rainfall for the period is "<<totalSum/(12.0*n)<<" inches"<<endl;
   /*Printing the reault for a period*/  
}

Output:

Indentation:


Related Solutions

IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Design a program that uses Nested loops to collect data and calculate the average rainfall over...
Design a program that uses Nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the numbers of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
Language: c++ works in visual basic Write a program that uses an array of nested structs...
Language: c++ works in visual basic Write a program that uses an array of nested structs to store the addresses for your store’s customers.  Each customer has a name and two addresses: home address and business address.  Each address has a street, city, state, and zip code. Requirements: 1. Data structure a. Define an Address struct with street, city, state and zip fields b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields...
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
using C++ 24. Using Files—Total and Average Rainfall Write a program that reads in from a...
using C++ 24. Using Files—Total and Average Rainfall Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this: During the months of March–June the total rainfall was 7.32 inches and the average...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT