Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

by C++please? . each version should be separated Thank you Version 1 (all intera

ID: 670121 • Letter: B

Question

by C++please? . each version should be separated Thank you

Version 1 (all interactive). 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, 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.

Version 2 (combines interactive and file output). For a more elaborate

version, also allow the user to request that the table and graph be output

to a file. The file name is entered by the user. This program does

everything that the Version 1 program does, but has this added feature. To

read a file name, you must use material presented in the optional section

of Chapter 5 entitled “File Names as Input.”

Version 3 (all I/O with files). This version is like Version 1 except that

input is taken from a file and the output is sent to a file. Since there is

no user to interact with, there is no loop to allow repeating the

display; both the table and the graph are output to the same file. If this

is a class assignment, ask your instructor for instructions on what file

names to use.

Explanation / Answer

// Display all the recorded data
void displayAll(struct monthly_rainfall *head){
while(head == NULL) return;
cout << head->month << " " << head->year << " " << head->rainfall_inch << endl;
displayAll(head->next);
}

// Finds and changes the rainfall data for a given month and year. The return value should be 0 if it was able to find the entry and -1 if no entry could be found.
int changeEntry(struct monthly_rainfall *head, int year, int month, float rainfall){
if(head == NULL) return -1;
if(head->month == month && head->year == year) head->rainfall_inch = rainfall;
return 0;
}

// Find the entry with highest monthly rainfall in the logged data and return a pointer to this structure
struct monthly_rainfall *findHighestRainfall(struct monthly_rainfall *head){
if(head->next == NULL) return head;
prev = findHighestRainfall(head->next);
if(head->rainfall_inch > prev->rainfall_inch) return head;
return prev;
}

// Find the entry with lowest monthly rainfall in the logged data and return a pointer to this structure
struct monthly_rainfall *findLowestRainfall(struct monthly_rainfall *head){
if(head->next == NULL) return head;
prev = findLowestRainfall(head->next);
if(head->rainfall_inch > prev->rainfall_inch) return head;
return prev;
}


// Calculate the monthly average over all the logged data
float calcAverageRainfall(struct monthly_rainfall *head){
int sum =0;

while( head != NULL) sum += head->rainfall_inch, head = head->next;

return sum*1.0/12;

}