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

using DevC++ Write a program that uses nested loops to collect data and calculat

ID: 3824495 • Letter: U

Question

using DevC++

Write a program that uses nested loops to collect data and calculate the sale for textbooks 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 number of textbooks sold that month. After all iterations, the program should display the number of months, the total number of textbooks, and the average number of textbooks sold each year and the average number of textbooks sold each year. Input validation: Do not accept a number less than 1 for the number of years. Do not accept negative numbers for the monthly sales quantity.

Explanation / Answer

#include <iostream>
using namespace std;

int main() {
int year,i,j,k,qty;
double avgBook;
qty=0;
   std::cout << "Enter the number of years" << std::endl;
   std::cin >> year ;
   if(year<1)
   {
   cout<< "Enter the valid number of years(>1)"<<endl;
   cin>>year;
   }
   for(i=1;i<=year;i++) // Nested outer loop
   {
       cout<<"inputs for the year "<<i;
      
       cout<<"-----------------";
       cout<<endl;
      
   for(j=1;j<=12;j++) // inner loop
   {
     
   k=0;
   if(j==1)
   {
   cout<< "Enter the number of text books sold in 1st month"<<endl;
   }
   if(j==2)
   {
   cout<< "Enter the number of text books sold in 2nd month"<<endl;
   }
   if(j==3)
   {
   cout<< "Enter the number of text books sold in 3rd month"<<endl;
   }
   if(j>3)
   {
   cout<< "Enter the number of text books sold in "<< j;
   cout<<"th month"<<endl;
   }
  
   cin>>k;
   if(k<0)
   {
   cout<< "Enter the valid number of sold quantity(+ve) for this month"<<endl;
   cin>>k;
   }
   qty=qty+k;
   }
  
   }
   avgBook =( (double)qty)/year;
  
   cout<< "The total number of months are "<<year*12;
   cout<<endl;
   cout<< "The total number of sold textbooks are "<<qty;
   cout<<endl;
   cout<< "The average number of sold textbooks per year are "<<avgBook;
   cout<<endl;
  
   return 0;
}