Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
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++, Chapter 5, Problem 1PP , additional homework tip  1

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

Absolute C++, 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
The Research team led by Bernadette Wolowitz at Cal-tech University has discovered a new Amoeba that grows in the order of a Fibonacci series every month. They are exhibiting this amoeba in a national. conference. They want to know the size of the amoeba at a particular time instant. If a particular month's index is given, write a program to display the amoeba's size. For Example, the size of the amoeba on month 1, 2, 3, 4, 5, 6,... will be 0, 1, 1, 2, 3, 5, 8.... respectively.
f. A laborant in a laboratory does a number of antigen tests in one day. Write a program that simulates the number of antigen tests done in a day in a laboratory, displays the number of positives and negatives, and calculates the % of positives in one day. Your program needs to first generate a random number that will show the number of tests done in a day. One laboratory in a day cannot do more than 100 tests and less than 1 (they do at least one). Then for each of the tests, your program needs to generate a random number to show whether they are positive or negative. In order to write this program, you need to have the following three functions used by the main function: test result (void): This function generates a test result. It should randomly return either one or zero. 1 means positive test result and 0 means negative test result. float positive percentage (int,int) : This function takes the total number of positives and the total number of tests and returns the % of positives.…
Consider records of employees each consists of employee's first name (string), number of hours worked (int)and hourly rate in Bahraini Dinar BD. (double).Write a program that reads from the user multiple records for multiple employees and displays the following onscreen:(a) the salary for each employee (Salary = Hours Worked x Hourly rate.)(b) the number of employees(c) the highest salary with employee name.Reading records of employees should stop when the user enter `stop' for the employee name. Sample I/O of theprogram is shown in the example below.

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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY