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

For the below code, I am able to enter the rainfall values for each month and ab

ID: 3913438 • Letter: F

Question

For the below code, I am able to enter the rainfall values for each month and able to display the total and average rainfall. When I use the minRainfall = min(newList) and maxRainfall = max(newList) then the output is 2.0 for both. I am trying to display the minimum and maximum rainfall along with the month.

##Write a program that allows the user to enter the total rainfall for each
##of the 12 months into a 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 rainfall amounts.

def main():
months = ['January', 'February' , 'March' , 'April' , 'May', 'June', 'July',
'August' , 'September', 'October', 'November', 'December']

total = 0

for months in list(months):
newList = []
amount = float(input("Enter the total monthly rainfall in inches: "))
newList.append(amount)

for values in list(newList):
total += values
print(months, newList)
print("Updated rainfall total for the year:" ,format(total, '.3f'))
average = total / 12
print("The average rainfall for the year is" ,format(average, '.3f'))

minRainfall = min(newList)
##indexMinMonth = newList.index(min(newList))
print(str(minRainfall))
maxRainfall = max(newList)
##indexMaxMonth = newList.index(max(newList))
print(str(maxRainfall))
  
  
main()

Explanation / Answer

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

int main ()

{

const int MONTHS = 12;

string name[MONTHS]= {"January","February","March","April","May","June","July","August","September","October","November","December"};

int count= 0;

double rain[MONTHS];

double avg;

double year=0;

double highest;

string highMonth;

double lowest;

string lowMonth;

for(count = 0; count < MONTHS; count++) // ask user to enter amount of rainfall for each month

{

cout <<"How many inches of rain does "<< name[count];

cout<< " have? ";

cin >> rain[count];

while (rain[count] < 0)

{

cout << "Please enter a number greater than 0."<< endl;

cin >> rain[count];

}

}

for(int count=0; count<MONTHS; count++) // totals up all the rainfall

year += rain[count];

avg = year / MONTHS;

for( int count = 0; count < MONTHS; count++) //spits out each month with its amount of rain

{

cout << name[count];

cout<< " has ";

cout<< rain[count] << " inches of rainfall. ";

}

highest = rain[0]; // finds month with the highest amount of rain

for (count = 1 ;count < MONTHS; count++)

{

if (rain[count] > highest)

{

highMonth = name[count];

highest = rain[count];

}

}

lowest = rain[0]; // finds month with the least amount of rain

for (count = 1 ;count < MONTHS; count++)

{

if (rain[count] < lowest)

{

lowMonth = name[count];

lowest = rain[count];

}

}

cout << endl;

cout << setprecision(2) << fixed;

cout <<"Total Rainfall throughout the year is " <<year << " inches" << endl;

cout <<"The average monthly rainfall is " << avg << " inches"<< endl;

cout <<"The month with the highest amount of rainfall is " << highMonth << " with " << highest<< " inches."<< endl;

cout <<"The month with the lowest amount of rainfall is " << lowMonth << " with " << lowest<< " inches."<< endl;

return 0;

}

There are some things that can be shortened down yes. For example, you dont need 2 individual for-loops to find the lowest and highest. Here is the full code

const int MONTHS = 12;

int count = 0;

string name[MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

double rain[MONTHS], avg, year = 0, highest, lowest;

string highMonth, lowMonth;

for (count = 0; count < MONTHS; count++) // ask user to enter amount of rainfall for each month

{

cout << "How many inches of rain does " << name[count] << " have? ";

cin >> rain[count];

while (rain[count] < 0)

{

cout << "Please enter a number greater than 0." << endl;

cin >> rain[count];

}

}

highest = rain[0]; // finds month with the highest amount of rain

lowest = rain[0]; // finds month with the least amount of rain

for (count = 1; count < MONTHS; count++)

{

if (rain[count] > highest)

{

highMonth = name[count];

highest = rain[count];

}

else if (rain[count] < lowest)

{

lowMonth = name[count];

lowest = rain[count];

}

}

avg = year / MONTHS;

for (count = 0; count < MONTHS; count++)

{

year += rain[count];

cout << name[count] << "has " << rain[count] << " inches of rainfall. ";

}

cout << endl << setprecision(2) << fixed;

cout << "Total Rainfall throughout the year is " << year << " inches" << endl;

cout << "The average monthly rainfall is " << avg << " inches" << endl;

cout << "The month with the highest amount of rainfall is " << highMonth << " with " << highest << " inches." << endl;

cout << "The month with the lowest amount of rainfall is " << lowMonth << " with " << lowest << " inches." << endl;