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

Linked Lists in C++ Rainfall Statistics Scientists at ABC Meteorology, Inc. are

ID: 3863712 • Letter: L

Question

Linked Lists in C++

Rainfall Statistics

Scientists at ABC Meteorology, Inc. are doing some research on global warming and they need to keep track of rainfall. They have chosen to hire you to help them out!

You’ve been asked to write a program that lets the user enter the total rainfall for an unknown (ahead of time) number of months into a linked list.  The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.

Special considerations:

·       Your program must allow the user to decide how many months of data will be entered.

·       Your linked list should hold the monthly data.

The expected output is provided below:

How many months will you enter? 3
Enter the rainfall (in inches) for month #1: 4
Enter the rainfall (in inches) for month #2: 2
Enter the rainfall (in inches) for month #3: 5

The total rainfall for the period is 11.00 inches.
The average rainfall for the period is 3.67 inches.
The largest amount of rainfall was 5.00 inches in month 3.
The smallest amount of rainfall was 2.00 inches in month 2.

Explanation / Answer

Please refer below code

#include<iostream>
#include<iomanip>

using namespace std;

typedef struct node
{
int month;
double total_rainfall;
struct node *next;
}node;
//creating linked list
void create_list(node **head, node *temp)
{
if(*head == NULL)
*head = temp;

else
{
node *travel = *head;

while(travel->next != NULL)
travel = travel->next;

travel->next = temp;
}
}
//Finding total rainfall in all months
double tot_rainfall(node *head)
{
node *travel = head;
double rainfall=0;

while(travel != NULL)
{
rainfall += travel->total_rainfall;
travel = travel->next;
}
return rainfall;
}
//finding node with largest rainfall
node *large_rainfall(node *head)
{
node *travel = head;
node *result = travel;

while(travel != NULL)
{
if(travel->total_rainfall > result->total_rainfall)
result = travel;

travel = travel->next;
}
return result;
}
//finding node with smallest rainfall
node *small_rainfall(node *head)
{
node *travel = head;
node *result = travel;

while(travel != NULL)
{
if(travel->total_rainfall < result->total_rainfall)
result = travel;

travel = travel->next;
}
return result;
}
int main()
{
int months;
double rainfall;
double total_rainfall,avg_rainfall;
node *largest_rainfall;
node *smallest_rainfall;
node *head = NULL;

cout<<"How Many months will you enter ? ";
cin>>months;

for(int i = 0; i < months; i++)
{
cout<<" Enter the rainfall (in inches) for month #"<<(i+1)<<":";
cin>>rainfall;
node *temp ;
temp = new node;
temp->month = (i+1);
temp->total_rainfall = rainfall;
temp->next = NULL;
create_list(&head,temp); //creating linked list
}
total_rainfall = tot_rainfall(head);
cout<<"The total rainfall for the period is "<<std::fixed<<std::setprecision(2)<<total_rainfall<<" inches"<<endl;

avg_rainfall = total_rainfall/months;
cout<<"The average rainfall for the period is "<<std::fixed<<std::setprecision(2)<<avg_rainfall<<" inches"<<endl;

largest_rainfall = large_rainfall(head);
cout<<"The largest amount of rainfall was "<<std::fixed<<std::setprecision(2)<<largest_rainfall->total_rainfall<<" inches"<<" in month "<<largest_rainfall->month<<endl;

smallest_rainfall = small_rainfall(head);
cout<<"The largest amount of rainfall was "<<std::fixed<<std::setprecision(2)<<smallest_rainfall->total_rainfall<<" inches"<<" in month "<<smallest_rainfall->month<<endl;
return 0;
}

Please refer below snapshot of output

How Many months will you enter ? 3

Enter the rainfall (in inches) for month #1:4

Enter the rainfall (in inches) for month #2:2

Enter the rainfall (in inches) for month #3:5
The total rainfall for the period is 11.00 inches
The average rainfall for the period is 3.67 inches
The largest amount of rainfall was 5.00 inches in month 3
The largest amount of rainfall was 2.00 inches in month 2

Process returned 0 (0x0) execution time : 7.244 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote