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++ -TEXT
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)
- Help Me With C Programming. At Lili's birthday party, there is a game arranged by Jojo as the host. The game is handing out a number of Y candies to a number of X people where all the sweets taste sweet except for the last one candy that tastes like rotten beans. The distribution of sweets will be sequential starting from position Z and if it passes the last position then the distribution of candy continues to the first position. Write down the person in the position of who will get the last candy. Format Input The first line of input is T, which is the number of test cases. The second row and the next number of T lines are X, Y, Z. X is the number of people who will be handed out candy. Y is the number of candies available. Z is the initial position of the person who will be handed out the candy. Format Output A string of "Case #N: " and a number that is the position of the person who got the last candy [Look at Image] In the sample above, for example, which is inputted in the…arrow_forwardQuestion 3 Write a program that finds the equivalent series and parallel resistance for a collection of resistor values. Your program should first read in the number of resistors and then compute the equivalent series resistance for all resistors in the collection and the equivalent parallel resistance. For example, if there are 3 resistors of 100, 200, and 300 Ohms, respectively, their equivalent series resistance is 100+200+300 and their equivalent parallel resistance is 1(1/100+1/200+1/300). Your program should read each resistance value (R), add R to the series sum (RS) and add 1/R to the parallel sum (RP). After loop exit, display RS and 1/RP.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
- Write a program in C++ that calculates the total grade of a student for N number of quizzes. Read value of N from user, and then ask the student marks in quiz 1, and total marks of quiz 1 and so on till N. Display the total of all the quizzes marks and the total marks as given below. Calculate the overall percentage (sum of the total points earned divided by the total points possible) and output it as a percentage. Sample input and output: How many quizzes? 3 Input your marks in quiz 1: 10 Total marks of quiz 1: 10 Input your marks in quiz 2: 7 Total marks of quiz 2: 12 Input your marks in quiz 3: 5 Total marks in quiz 3: 8 Your total is 22 out of 30, and percentage is 73.33%.arrow_forwardA supermarket is doing a sales promotion for soft drinks. If one day you buy soft drinks and bring the empty bottles the next day, they exchange each set of K empty bottles for a full one. A customer wants to make the most of this offer and therefore bought several bottles on the first day of the promotion. Now he wants to know how many bottles he will have at the end of the second day of the promotion if he use it to the fullest. Make a program to calculate this. Input The first input line contains integer T (1 ≤ T ≤ 10000), which indicates the number of test cases. In each of the following T lines come two integers N and K (1 ≤ K, N ≤ 10000), respectively the number of soft drinks bought and the number of empty bottles to gain a full. Output For each test case print the number of bottles that the customer will have on the second day, if he makes the most of the offer.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
- write a program that displays, in ascending order,the first names of those students who have attended more than one day of the course.If there are students with the same first name, display their first and last names.arrow_forwardCODE MUST BE IN PYTHON.arrow_forwardYou are tasked with writing a program to process students’ end-of-semester marks. For each student you must enter into your program the student’s id number (an integer), followed by their mark (an integer out of 100) in 4 courses. A student number of 0 indicates the end of the data. A student moves on to the next semester if the average of their 4 courses is at least 60 (greater than or equal to 60). Write a program to read the data and print, for each student: The student number, the marks obtained in each course, final mark of the student (average of all the marks), their final grade (A,B,C,D or F) and whether or not they move on to the next semester. In addition, after all data entry is completed, print The number of students who move on to the next semester and the number who do not. The student with the highest final mark (ignore the possibility of a tie). Grades are calculated as follows: A = 70 and over, B = 60 (inclusive) -70, C = 50(inclusive)-60 D =…arrow_forward
- Write a program that take 5 digit number as an input from the user and perform the following tasks:1. Split a number into digits.2. Find the sum of the all the digits in the number.3. Find the product of the all the digits in the number.4. Find maximum number with in the number along with position.5. Printing the digits in the reverse order.Note: Make user defined functions to perform each task.arrow_forwardHi, I need help with the following assignment. Implement a program that reads in a year and outputs the approximate value of a Ferrari 250 GTO in that year. Use the following table that describes the estimated value of a GTO at different times since 1962. Year Value 1962-1964 $18,500 1965-1968 $6,000 1969-1971 $12,000 1972-1975 $48,000 1976-1980 $200,000 1981-1985 $650,000 1986-2012 $35,000,000 2013-2014 $52,000,000arrow_forward2. Pure Gems Store sells different varieties of gems to its customers. Emerald, Ivory, Jasper, Ruby, Garnet and their prices are 1760, 2119, 1599, 3920, 3999 respectively. Write a Python program to calculate the bill amount to be paid by a customer based on the list of gems and quantity purchased. Any purchase with a total bill amount above in python Rs.30000 is entitled for 5% discount. If any gem required by the customer is not available in the store, then consider total bill amount to be -1. Assume that quantity required by the customer for any gem will always be greater than 0. Perform case-sensitive comparison wherever applicable.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