Question: Having trouble with this question. Please use template. Thank you!! Write a program that reads in the average monthly rainfall for a city for each

Having trouble with this question. Please use template. Thank you!!

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 this 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 7.8

(

The Function graph The complete program for producing the desired bar graph is shown in Display 7.8. We have not taken you step-by-step through the design of the function graph because it is quite straightforward.

Display 7.8 Production Graph Program 1 //Reads data and displays a bar graph showing productivity for each plant. 2 #include 3 #include 4 const int NUMBER_OF_PLANTS = 4;

5 void inputData(int a[], int lastPlantNumber); 6 //Precondition: lastPlantNumber is the declared size of the array a. 7 //Postcondition: For plantNumber = 1 through lastPlantNumber: 8 //a[plantNumber 1] equals the total production for plant number plantNumber.

9 void scale(int a[], int size); 10 //Precondition: a[0] through a[size 1] each has a nonnegative value. 11 //Postcondition: a[i] has been changed to the number of 1000s (rounded to 12 //an integer) that were originally in a[i], for all i such that 0 <= i <= size 1.

13 void graph(const int asteriskCount[], int lastPlantNumber); 14 //Precondition: asteriskCount[0] through asteriskCount[lastPlantNumber 1] 15 //have nonnegative values. 16 //Postcondition: A bar graph has been displayed saying that plant 17 //number N has produced asteriskCount[N 1] 1000s of units, for each N such that 18 //1 <= N <= lastPlantNumber 19 void getTotal(int& sum); 20 //Reads nonnegative integers from the keyboard and 21 //places their total in sum. 22 int roundNum(double number); 23 //Precondition: number >= 0. 24 //Returns number rounded to the nearest integer. 25 void printAsterisks(int n); 26 //Prints n asterisks to the screen. 27 int main( ) 28 { 29 using namespace std; 30 int production[NUMBER_OF_PLANTS]; 31 cout << "This program displays a graph showing " 32 << "production for each plant in the company. "; 33 inputData(production, NUMBER_OF_PLANTS); 34 scale(production, NUMBER_OF_PLANTS); 35 graph(production, NUMBER_OF_PLANTS); 36 return 0; 37 } 38 //Uses iostream: 39 void inputData(int a[], int lastPlantNumber) 40 //Uses iostream: 41 void getTotal(int& sum) 42 void scale(int a[], int size) 43 //Uses cmath: 44 int roundNum(double number) 45 //Uses iostream: 46 void graph(const int asteriskCount[], int lastPlantNumber) 47 { 48 using namespace std; 49 cout << " Units produced in thousands of units: "; 50 for (int plantNumber = 1; 51 plantNumber <= lastPlantNumber; plantNumber++) 52 { 53 cout << "Plant #" << plantNumber << " "; 54 printAsterisks(asteriskCount[plantNumber 1]); 55 cout << endl; 56 } 57 } 58 //Uses iostream: 59 void printAsterisks(int n) 60 { 61 using namespace std; 62 for (int count = 1; count <= n; count++) 63 cout << "*"; 64 }

)

Download Display 7.8, 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 she or he 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.

//Program is to //1)read in long term month by month average rain fall // averages //2)read in the previous year's month by month actual rain // fall. //Output: //'nicely' formatted table showing rainfall for each of the // previous //12 months as well as how much above or below the monthly // average the rainfall is for each month. The output // should correctly label months. // //Month name handling: code months as integers and do a //conversion at output. Any input scheme acceptable as long //as the user interface is relatively easy to use.

//Remarks: If the current month is April, we don't have //April's rainfall figure yet, so we go back to April of the //previous year, to March of this year.

//Enhanced version: //Add capability to print two bar graphs for each month, //showing the average rainfall and the actual rainfall for //each of the previous 12 months. Ask the user's preference: //a table or a graph, and display the format requested. // //Provide a loop to repeat the process at the user's request.

#include #include #include

struct monthly_rain { int month; double rain_fall; double avg_rain_fall; };

//declaration of function from file Ch10Prg1.bargraph.cpp void output_bargraph( monthly_rain rainfall[], int current_month ); //output: scale and bargraph of month by month average and //actual rainfall.

void input( monthly_rain rainfall[], int& year ); //precondition: //routine called correctly, with variable arguments //postcondition: //User is asked for current month, then is asked for //the average and actual rainfall for each of the //preceding 12 months.

void output_table( monthly_rain rainfall[], int& year ); //Precondition: //input is array of size 12 of monthly_rain structs. //Postcondition: output to screen is formatted table showing //rainfall for each of the previous 12 months as well as how //much above or below the monthly average the rainfall //for each month. The output should correctly label months.

int main() { using namespace std; int current_month; monthly_rain rainfall[12]; char ans; char tg; do { input( rainfall, current_month); cout << "Please choose between g)raphical and t)abular " << "output" << endl; cin >> tg; //tg means table/graphics choice :)

if ( 't' == tg || 'T' == tg ) { cout << "You chose the tabular output." << endl; output_table( rainfall, current_month ); } else if ( 'g' == tg || 'G' == tg ) { cout << "You chose the graphical output" << endl; output_bargraph( rainfall, current_month); } else { cout << "You typed neither of the choices. " << "Defaulting to graphical output." << endl; output_bargraph( rainfall, current_month ); } cout << "Y/y continues, any thing else quits"; cin >> ans; }while( 'y' == ans || 'Y' == ans ); return 0; }

void full_month_name(int);

void input( //Complete the argument list) { // Complete the function cout << " Monthly rainfall input routine." << endl << endl << "Please enter the average rainfall for each month " << endl;

cout << " Average rainfall: " << endl;

cout << endl << "The actual rainfall: " << endl; cout << "What is the current month? Please give the " << "number of the month (Jan = 0, etc." << endl;

cout << " The current month is: " ; full_month_name(current_month); cout << endl;

cout << "Please enter the actual rainfall for " << "each month, as prompted, First for the " << "months in the previous year: " << endl;

cout << "Now for the months in this year: " << endl; } //end of input routine

//helping routines for output_table, defined at the end of //this file. void month_name( int month ); void full_month_name( int month);

void output_table(//Complete the argument list) { // Complete the function using namespace std; int i; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); //heading: Monthly Rainfall // For the 12 Months Preceding ..... // Actual, Average, and Difference (= Actual - Average)

}

void month_name( int month ) { // Complete the function switch (month) { } }

void full_month_name( int month ) { // Complete the function switch (month) { }

// //to print two bargraphs for each month, showing average and //actual rainfall each month.

void full_month_name( int ); void month_name( int );

//outputs x asterisks, no void bargraph( double x ) { // Complete the function }

//output scale followed by // void output_scale() { using namespace std; cout << " Rainfall "; for ( int i = 0; i < 14; i++) { double j = i/2.0; cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(1); cout << j << " "; } cout << endl << "\t "; for (i = 0; i < 14; i++) cout << "|****"; cout << endl; }

void output_bargraph( //Complete the argument list ) { // Complete the function }

/* There follows edited output obtained from this run.

Monthly rainfall input routine.

Please enter the average rainfall for each month Average rainfall: For month January Avg Rainfall: 3.7 For month February Avg Rainfall: 3.6 For month March Avg Rainfall: 5.1 For month April Avg Rainfall: 3.8 For month May Avg Rainfall: 4.2 For month June Avg Rainfall: 4.2 For month July Avg Rainfall: 4.4 For month August Avg Rainfall: 4.8 For month September Avg Rainfall: 4 For month October Avg Rainfall: 3.3 For month November Avg Rainfall: 3.3 For month December Avg Rainfall: 3.5

The actual rainfall: What is the current month? Please give the number of the month (Jan = 0, etc. The current month is: May Please enter the actual rainfall for each month, as prompted, First for the months in the previous year: For month May Rainfall: 4.3 For month June Rainfall: 3.2 For month July Rainfall: 5.5 For month August Rainfall: 5.1 For month September Rainfall: 4 For month October Rainfall: 2.2 For month November Rainfall: 2 For month December Rainfall: 1.1 Now for the months in this year: For month January Rainfall: 4.1 For month February Rainfall: 3.3 For month March Rainfall: 3.9 For month April Rainfall: 3.7 Please choose between g)raphical and t)abular output You chose the tabular output. Monthly Rainfall For the 12 Months Preceding May Actual, Average, and Difference (= Actual - Average) -----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+---- Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec -----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+---- average 4.1 | 3.3 | 3.9 | 3.7 | 4.3 | 3.2 | 5.5 | 5.1 | 4.0 | 2.2 | 2.0 | 1.1 actual 3.7 | 3.6 | 5.1 | 3.8 | 4.2 | 4.2 | 4.4 | 4.8 | 4.0 | 3.3 | 3.3 | 3.5 diffs 0.4 |-0.3 |-1.2 |-0.1 | 0.1 |-1.0 | 1.1 | 0.3 | 0.0 |-1.1 |-1.3 |-2.4 P)rev yr | | | | P | P | P | P | P | P | P | P

Y/y continues, any thing else quits Monthly rainfall input routine.

Please enter the average rainfall for each month Average rainfall: For month January Avg Rainfall: 3.7 . . . . For month December Avg Rainfall: 3.5

The actual rainfall: What is the current month? Please give the number of the month (Jan = 0, etc. The current month is: May Please enter the actual rainfall for each month, as prompted, First for the months in the previous year: For month May Rainfall: 4.3 . . . . For month December Rainfall: 1.1 Now for the months in this year: For month January Rainfall: 4.1 . . . . For month April Rainfall: 3.7 Please choose between g)raphical and t)abular output You chose the graphical output

Rainfall 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 |****|****|****|****|****|****|****|****|****|****|****|****|****|**** January average ************************************** actual ***************************************** February average ************************************* actual ********************************* March average *************************************************** actual *************************************** April average ************************************** actual ************************************** May (Previous year) average ******************************************* actual ******************************************* June (Previous year) average ******************************************* actual *********************************

Rainfall 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 |****|****|****|****|****|****|****|****|****|****|****|****|****|**** July (Previous year) average ********************************************* actual ******************************************************* August (Previous year) average ************************************************ actual *************************************************** September(Previous year) average **************************************** actual **************************************** October (Previous year) average ********************************* actual *********************** November(Previous year) average ********************************* actual ******************** December(Previous year) average *********************************** actual ************

Rainfall 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 |****|****|****|****|****|****|****|****|****|****|****|****|****|**** Y/y continues, any thing else quits */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!