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
Introduction To Programming Using Visual Basic (11th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Web Development and Design Foundations with HTML5 (8th Edition)
Concepts Of Programming Languages
Using MIS (10th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
- Given 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_forwardI need this in Pythonarrow_forwardWrite 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_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
- CODE 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_forwardWrite 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_forward
- Teachers in most school districts are paid on a schedule that provides asalary based on their number of years of teaching experience.• For example, a beginning teacher in the Lexington School District might bepaid $30,000 the first year. For each year of experience after this first year,up to 10 years, the teacher receives a 2% increase over the preceding value.• Write a program that displays a salary schedule, in tabular format, for teachers in a school district.The inputs are:• Starting salary• Annual percentage increase• Number of years for which to print the schedule• Each row in the schedule should contain the year number and the salary for that yearAn example of the program input and output is below.arrow_forwardC LANGUAGEarrow_forwardGiven a long sentance, reverse each word in it and there order, so you the snetec can be read from right to left (see example below). The sentence will consist of words separated by a space, where the last word ends with a period. The sentence will only consist of a single period at the end to indicate the end of the sentence. Input: The first line of the input contains a positive integer t, which indicates the number of testcases the program will have to run. The first line is followed by t lines, each containing a separate testcase. Each testcase ti consists of strings separated by white spaces. Output: The output must contain t lines, where each line has the sentce returned in reversed order. which is the output of the calculation of the testcase ti. Sample outputs (user input is indicated as bold): 2 Hello world. dlroW olleH. I Love Coding. gnidoC evoL I.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