Absolute C++, Global Edition
Absolute C++, Global Edition
6th Edition
ISBN: 9781292098593
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 5, Problem 1PP

Write a program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program then prints out a nicely formatted table showing the rainfall for each of the previous 12 months as well as how much above or below average the rainfall was for each month. The average monthly rainfall is given for the months January, February, and so forth, in order. To obtain the actual rainfall for the previous 12 months, the program first asks what the current month is and then asks for the rainfall figures for the previous 12 months. The output should correctly label the months.

There are a variety of ways to deal with the month names. One straightforward method is to code the months as integers and then do a conversion before doing the output. A large switch statement is acceptable in an output function. The month input can be handled in any manner you wish, as long as it is relatively easy and pleasant for the user.

After you have completed the previous program, produce an enhanced version that also outputs a graph showing the average rainfall and the actual rainfall for each of the previous 12 months. The graph should be similar to the one shown in Display 5.4, except that there should be two bar graphs for each month, and they should be labeled as the average rainfall and the rainfall for the most recent month. Your program should ask the user whether he or she wants to see the table or the bar graph, and then should display whichever format is requested. Include a loop that allows the user to see either format as often as the user wishes until the user requests that the program end.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program plan:

  1. Necessary header files are included.
  2. Four functions table (),barGraph(),barAsterisks(),displayMonth () are declared for various functions and defined inside the main() function
  3. double actualRainfall[12],double avgRainfall[12], double statusRainfall[12],int currentMonth, char userChoice, int count variables are declared of different data types to store multiple values required in the program.
  4. For loops are used for various conditions.
  5. A do-while loop is used to exit from the program.
  6. A switch statement is used for printing the different month names according the choice of month.

Program Description:

The main purpose of the program is to compare the rainfall of 12 months in a city. Comparison of average monthly rainfall and actual monthly rainfall is shown either in tabular form or with the help of Bar Graph.

Explanation of Solution

Sample Program

/*Header files section*/
#include<iostream>
#include<stdlib.h>
#include<string>
#include <iomanip>
using namespace std;
//Function declaration
void table(double actualRainfall[], double avgRainfall[],
   double statusRainfall[]);
void barGraph(double avgRainfall[], double actualRainfall[],
   double statusRainfall[]);
void barAsterisks(int stars);
void displayMonth(int month);

//main method
int main()
{
   //Declare variables
   double actualRainfall[12];
   double avgRainfall[12];
   double statusRainfall[12];
   int currentMonth;
   char userChoice;
   int count=0;
   //Prompt and read each month's rainfall
   //from the user
   cout << "Enter average monthly "
       << "rainfalls for each month..." << endl;
   for (int i = 0; i < 12; i++)
   {
       displayMonth(i);
       cout << ": ";
       cin >> avgRainfall[i];
   }
     //Prompt and read the current month
   // and then asks for the rainfall figures
   //for the previous 12 months
   cout << "Enter the current month "
       "(For example: Enter 1 for January) : "
       << endl;
   cin >> currentMonth;
   cout << "Enter the actual rainfall for "
       " each month in the previous year..."
       << endl;
  
   for (int month = currentMonth - 1; count < 12;
       month = (month + 1) % 12, count++)
   {
       displayMonth(month);
       cout << ": ";
       cin >> actualRainfall[month];
   }
   for (int i = 0, j = 0; i < 12; i++, j++)
   {
       if (avgRainfall[i] > actualRainfall[i])
           statusRainfall[j] = actualRainfall[i] - avgRainfall[i];
       else
           statusRainfall[j] = actualRainfall[i] - avgRainfall[i];
   }
   cout << endl;
   //do-while loop to see either table or bar
   do
   {
       cout << "Enter your choice(T for table or "
           " B for bar-graph or E for exit program): ";
       cin >> userChoice;
       cout << endl;
       if (userChoice == 'T' || userChoice == 't')
           table(actualRainfall, avgRainfall, statusRainfall);
       else if (userChoice == 'B' || userChoice == 'b')
           barGraph(avgRainfall, actualRainfall, statusRainfall);
       else if (userChoice == 'E' || userChoice == 'e')
       {
           cout << "Exit the program...Thank you"<<endl;
               exit(0);
       }
       cout << endl;
   } while (true);
   return 0;
}
//Method definition of table
void table(double rainfall[], double avgRainfall[],
   double statusRainfall[])
{
   cout << endl;
   cout << "___________________________________________"
       "______________________________________" << endl;
   cout << "Month" << setw(20)
       << "Actual_Rainfall" << setw(20)
       << "Average_Rainfall "<< setw(25)
       << "    Above_or_Below_Average_Rainfall"
       << setw(20) << endl;
   cout << "___________________________________________"
       "______________________________________" << endl;
   for (int i = 0; i < 12; i++)
   {
       cout.setf(ios_base::left);
       displayMonth(i);
       cout << "\t" << setw(20) << rainfall[i] << setw(20)
           << avgRainfall[i] << setw(20) << statusRainfall[i];
       cout << endl;
   }
   cout << "_____________________________________________"
       "____________________________________" << endl;
}
//Method definition of barGraph
void barGraph(double avgRainfall[], double rainfall[], double statusRainfall[])
{
   cout << "Graph showing the average rainfall "
       "and the actual rainfall for each of the "
       "previous 12 months..." << endl;
   cout << endl;
   for (int i = 0; i < 12; i++)
   {
       displayMonth(i);
       cout << ":" << "Average_Rainfall" << " ";
       int n1 = (int)avgRainfall[i];
       barAsterisks(n1);
       cout << endl;
       displayMonth(i);
       cout << ":" << "Actual_Rainfall" << " ";
       int n = (int)rainfall[i];
       barAsterisks(n);
       cout << endl;
       cout << endl;
   }
}
//Method definition of barAsterisks
void barAsterisks(int stars)
{
   for (int count = 1; count <= stars; count++)
       cout << "*";
}
//Method definition of displayMonth
void displayMonth(int month)
{
   cout.width(8);
   switch (month)
   {
   case 0:
       cout << "January";
       break;
   case 1:
       cout << "February";
       break;
   case 2:
       cout << "March";
       break;
   case 3:
       cout << "April";
       break;
   case 4:
       cout << "May";
       break;
   case 5:
       cout << "June";
       break;
   case 6:
       cout << "July";
       break;
   case 7:
       cout << "August";
       break;
   case 8:
       cout << "September";
       break;
   case 9:
       cout << "October";
       break;
   case 10:
       cout << "November";
       break;
   case 11:
       cout << "December";
       break;
   }
}

Explanation:

In the above program, user inputs the average and actual monthly rainfall for the 12 months. After that a comparison is made between the average and actual rainfall of the particular month. Different options are provided to the user to display the comparison of rainfalls of the months either in Tabular form or as Bar Graph. In that option only, user can choose to exit the program by choosing the provided option. In the last according to the choice of user, data is displayed either in Tabular form or as Bar Graph.

Sample Output:

Absolute C++, Global Edition, Chapter 5, Problem 1PP , additional homework tip  1

Absolute C++, Global Edition, Chapter 5, Problem 1PP , additional homework tip  2

Absolute C++, Global Edition, Chapter 5, Problem 1PP , additional homework tip  3

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program that reads an integer age of kids, finds the maximum of them, and counts its occurances, finds the minimum of them, and counts its occurances. And Find the average ages. Assum that the input ends with number -1.
Write a program that prompts for and reads the radius and the height of a cylinder (in centimeters). It then computes its volume by using pass by reference and pass by value
Write a program that receives a number from a user and checks if it's float or integer.In case of being float, print for the user that his/her number is float with integer part andfractional part. In case of being integer, print to the user that his/her number is aninteger and odd/even numbeR

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY