Write a
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.
Program plan:
- Necessary header files are included.
- Four functions table (),barGraph(),barAsterisks(),displayMonth () are declared for various functions and defined inside the main() function
- 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.
- For loops are used for various conditions.
- A do-while loop is used to exit from the program.
- 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:
Want to see more full solutions like this?
Chapter 5 Solutions
Absolute C++
Additional Engineering Textbook Solutions
Problem Solving with C++ (10th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
Starting Out with Python (4th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out with C++: Early Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- python cant be the samearrow_forwardPlease code this in Python according to the function given below.arrow_forwardf. 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.…arrow_forward
- 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.arrow_forwardUse the given functions and Listsarrow_forwardGiven a square matrix with the elements 0 or 1, write a program tofind a maximum square submatrix whose elements are all 1s. Your programshouldprompt the user to enter the number of rows in the matrix. The program then displaysthe location of the first element in the maximum square submatrix and thenumber of rows in the submatrix. Here is a sample run: Enter the number of rows in the matrix: 5 ↵EnterEnter the matrix row by row:1 0 1 0 1 ↵Enter1 1 1 0 1 ↵Enter1 0 1 1 1 ↵Enter1 0 1 1 1 ↵Enter1 0 1 1 1 ↵EnterThe maximum square submatrix is at (2, 2) with size 3 Your program should implement and use the following method to find the maximumsquare submatrix:public static int[] findLargestBlock(int[][] m)The return value is an array that consists of three values. The first two values arethe row and column indices for the first element in the submatrix, and the thirdvalue is the number of the rows in the submatrix.arrow_forward
- 1. Write a program to find the equivalent series and parallel resistance for 3 resistor values. Your program should scan the 3 resistor values and then compute the equivalent series and parallel resistance for all 3 resistors. For example, if the 3 resistor values are r1=100, r2=200 and r3=300 ohms, respectively, their equivalent series resistance is rl + r2 + r3 = 100 + 200 + 300 = 600 ohms and their equivalent parallel resistance = 1 / [1/rl + 1/r2 + 1/r3] = 1/[0.01+0.005+0.0033] = 54.55 ohms.arrow_forwardYou are working for a lumber company, and your employer would like a program that calculatesthe cost of lumber for an order. The company sells pine, fir, cedar, maple, and oak lumber.Lumber is priced by board feet. One board foot equals one square foot that is one inch thick. Theprice per board foot is given in the following table: (image 1) The lumber is sold in different dimensions (specified in inches of width and height, and feet oflength) that need to be converted to board feet. For example, a 2 x 4 x 8 piece is 2 inches wide, 4inches high, and 8 feet long, and is equivalent to 5.333 board feet (2 * 4 * 8 = 64, which whendivided by 12 = 5.333 board feet). An entry from the user will be in the form of a letter and fourinteger numbers. The integers are the number of pieces, width, height, and length. The letter willbe one of P, F, C, M, O (corresponding to the five kinds of wood) or T, meaning total. When theletter is T, there are no integers following it on the line. The program…arrow_forwardProblem: Write a program for computing the amount of money in a bank account at the end of each quarter for a period of one year. A quarter is equivalent to 3 months. Assume that the account is created at the beginning of a year (i.e. January 1). The year will be entered at run time through the keyboard. The account has an initial principal that is entered at run time through the keyboard. No deposit or withdrawal is made within the one-year period. The interest is compounded quarterly which means that the interest earned for the first quarter will form part of the principal for the second quarter, the interest earned for the second quarter will form part of the principal for the third quarter and so on. The annual interest rate is 5%. The interest rate for a quarter is computed by dividing the annual interest rate by 4 since there are four quarters in a year. The computational procedure (see algorithm) and the output of the program (see sample run) are shown below. The output includes…arrow_forward
- Homework write a program that will add the terms of an infinite geometric series. The program should read the first term (a) and the common ratio (r) for the series. It should the compute the sum in two ways: by formula (s=a/1-r), and by adding the individual terms until the answer agrees with the formula to 7 significant digits. The print the formula answer, the answer found by adding terms, and the number of terms that were added. Print the sums to at least ten places. Verify input with a while loop. Two real values are equal to n significant digits if the following condition is true: |a-b|<|a*10^-n| HELP ME FIX MY CODE comment in the picture. #include <iostream>#include <cmath>using namespace std; int main(){ int i = 1;double a, b, r, number, sumbyformula, sum2;cout<<"Enter a value of first term a: ";cin>>a;cout<<"Enter a value of ratio r: ";cin>>r;// Loop -1<r<1while(r>=1 and r<=-1){cout<< "Enter the number between -1 and 1…arrow_forwardGiven a number n, identify and print which in the given set of numbers are factors of n. Should there be no factors listed in the set of numbers, print "I'm alone". For example, given the number 36 and the set of numbers 2, 3, 5, 7, 12. Only print the numbers which are factors of 36, which are 2, 3, 12. Input The first line contains the number n; The second line contains how many numbers there are in the set of numbers; The third line contains the set of numbers. INPUT: 36 5 2·3·5·7·12 Output The set of numbers that are factors of n separated by a new line in order of appearance. If there are none, print "I'm alone" OUTPUT: 2 3 12arrow_forwardHomework write a program that will add the terms of an infinite geometric series. The program should read the first term (a) and the common ratio (r) for the series. It should the compute the sum in two ways: by formula (s=a/1-r), and by adding the individual terms until the answer agrees with the formula to 7 significant digits. The print the formula answer, the answer found by adding terms, and the number of terms that were added. Print the sums to at least ten places. Verify input with a while loop. Two real values are equal to n significant digits if the following condition is true: |a-b|<|a*10^-n| Wrong test - this won't work if r is very near one. Try a=2, r=.99999 With smaller values of r, it is asking for too much precision. With a=2,r=.99, it is getting 10 significant digits. HELP ME FIX CODE. PLEASE USE MY CODE FIX #include <iostream>#include <cmath>using namespace std; int main(){ //declare variables int terms; double a, r, number, sumbyformula, sum2;…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr