Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CSC121Module7Fall2021Student/~$C121Module7LabFall2020.docx CSC121Module7Fall2021Student/~$C121Module7LabSpring2021.docx CSC121Module7Fall2021Student/CSC121Module7LabFall2021.docx CSC121 Lab 7: Nested...

1 answer below »
CSC121Module7Fall2021Student/~$C121Module7LabFall2020.docx
CSC121Module7Fall2021Student/~$C121Module7LabSpring2021.docx
CSC121Module7Fall2021Student/CSC121Module7LabFall2021.docx
CSC121 Lab 7: Nested Loops
Name         
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 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.
You must test your program using the below data. Your output must look just like that in the test run below. Don’t forget to copy and paste your test run to the bottom of your source code. Name your program “YourLastNameLab7.cpp”.
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 XXXXXXXXXXand 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 XXXXXXXXXXand the average rainfall for the year is 3.414
Over a period of 24 months, XXXXXXXXXXinches of rain fell.
Average monthly rainfall for the period is 3.168 inches.
Answered Same Day Oct 19, 2021

Solution

Darshan answered on Oct 20 2021
128 Votes
#include include std li
ary
using namespace std;
main program
int main()
{
    
define input for Number of inches of rain for month
    float x;
    
define input for total overall rain
    float sum1=0,sum2=0;
    
define input for Number of years
    int choice;
    
User message
    cout
"This program will calculate average rainfall over a period of years. How many years do you wish to average?"
endl;
    
take input untill years must be at least one
    do{
    cin
choice;
        if(choice<1)
        {
            cout
"Years must be at least one. Please re-enter: ";
        }
    }while(choice<1);
    
define a
ay for Number of years total rain    
    float a
[choice];
    
outter for loop iteration for no of years
    for(int i=1;i<=choice;i++)
    {    
        cout
endl;
        cout
"Year "
i
endl;
            
iner for loop for each month
            for(int j =1;j<=12;j++)
            {            
                    
take input untill no of inches of rain must be zero or greater than zero
                    do{
                    cout
"Number of inches of rain for month "
j
"? ";
                    cin
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here