ABSOLUTE C++ -TEXT
ABSOLUTE C++ -TEXT
6th Edition
ISBN: 2810017515514
Author: SAVITCH
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++ -TEXT, Chapter 5, Problem 1PP , additional homework tip  1

ABSOLUTE C++ -TEXT, Chapter 5, Problem 1PP , additional homework tip  2

ABSOLUTE C++ -TEXT, 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
For C# (C Sharp): When an employee is hired they get a set starting salary. Every year the salary grows by 4%. In the 10th year of service an employee gets $15,000 bonus added to the current salary, but after that the salary will grow by 2% every year. Write a program fragment that takes number of years of service from a text box txtYears, and starting salary from txtStart, calculates salary for each year, and displays it in a list box IstSalary. Use data validation for the inputs to make sure they are numeric.
Transient PopulationPopulations are affected by the birth and death rate, as well as the number of people who move in and out each year. The birth rate is the percentage increase of the population due to births and the death rate is the percentage decrease of the population due to deaths. Write a program that displays the size of a population for any number of years. The program should ask for the following data: The starting size of a population P The annual birth rate (as a percentage of the population expressed as a fraction in decimal form)B The annual death rate (as a percentage of the population expressed as a fraction in decimal form)D The average annual number of people who have arrived A The average annual number of people who have moved away M The number of years to display nYears Write a function that calculates the size of the population after a year. To calculate the new population after one year, this function should use the formulaN = P + BP - DP + A - Mwhere N is the…
Write a program that reads from a student his/her name and his/her ID which is formed of 8 digits. The program should display the day of registration and the starting hour of registration according to the below tables. First 4 digits to the left Registration Day Monday Tuesday WEdnesday 2018 2019 2020 First digit to the right Registration IHour 4 6 10 7-8

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