Question

In: Computer Science

design a program that lets the user enter the total rainfall for each of 12 months...

design a program that lets the user enter the total rainfall for each of 12 months into an array. 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.  

---->>> Enhance the program so it sorts the array in ascending order and displays the values it contains.

You can choose which sorting algorithm to use (bubble sort, selection sort, or insertion sort). Depending on your choice, you must implement one of the following functions:

def bubble_sort(months, months_len)

def selection_sort(months, months_len)

def insertion_sort(months, months_len)

Example Run

Please enter the rainfall for month [ January ]

15.5

Please enter the rainfall for month [ February ]

17.2

Please enter the rainfall for month [ March ]

18.0

Please enter the rainfall for month [ April ]

22.2

Please enter the rainfall for month [ May ]

15.9

Please enter the rainfall for month [ June ]

2.2

Please enter the rainfall for month [ July ]

0

Please enter the rainfall for month [ August ]

0

Please enter the rainfall for month [ September ]

2.1

Please enter the rainfall for month [ October ]

8.5

Please enter the rainfall for month [ November ]

12.0

Please enter the rainfall for month [ December ]

15.5

Total Rainfall: 129.10000000000002

Average Monthly Rainfall: 10.758333333333335

Month with highest rainfall: April

Month with lowest_rainfall: July

Monthly Rainfall Sorted (ascending):

0 inches

0 inches

2.1 inches

2.2 inches

8.5 inches

12.0 inches

15.5 inches

15.5 inches

15.9 inches

17.2 inches

18.0 inches

22.2 inches


_____________________________________________________________________________________________________________

MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

NUMBER_OF_MONTHS = 12

def get_total_rainfall(months, months_len):

"""

Module Name: get_total_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Iterates over the array and calculates the total rainfall

"""

# Python Shortcut:

#total_rainfall = sum(months)

total_rainfall = 0

# Iterate of the array of months to accumulate the total.

for index in range(0, months_len):

total_rainfall += months[index]

return total_rainfall

def get_average_monthly_rainfall(months, months_len):

"""

Module Name: get_average_monthly_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Calculates and returns the average monthly rainfall based on the 'months' array

"""

# Average is calculated by dividing the total by the number of elements.

return get_total_rainfall(months, months_len) / months_len

def get_month_with_highest_rainfall(months, months_len):

"""

Module Name: get_month_with_highest_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Finds and returns the month with the highest rainfall.

Returns the first month if more than one month qualify as 'highest'

"""

# Sequentially search the array to find the maximum

max_index = 0

for index in range(1, months_len):

if months[max_index] < months[index]:

max_index = index

return max_index

def get_month_with_lowest_rainfall(months, months_len):

"""

Module Name: get_month_with_highest_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Finds and returns the month with the lowest rainfall.

Returns the first month if more than one month qualify as 'lowest'

"""

# Sequentially search the array to find the minimum

min_index = 0

for index in range(1, months_len):

if months[min_index] > months[index]:

min_index = index

return min_index


def main():

"""

Module Name: main

Parameters: None

Description: Program entry point. Provides the program flow of execution

"""

months = [0.0] * NUMBER_OF_MONTHS

# Input

# Prompt the user to collect the rainfall for each month of the year

for index in range(0, NUMBER_OF_MONTHS):

print("Please enter the rainfall for month [", MONTH_NAMES[index], "]")

rainfall = float(input())

months[index] = rainfall

# Processing

# Calculate the attributes per the assignment

total_rainfall = get_total_rainfall(months, NUMBER_OF_MONTHS)

average_rainfall = get_average_monthly_rainfall(months, NUMBER_OF_MONTHS)

highest_rainfall_index = get_month_with_highest_rainfall(months, NUMBER_OF_MONTHS)

lowest_rainfall_index = get_month_with_lowest_rainfall(months, NUMBER_OF_MONTHS)

# Output

# Display the results

print("Total Rainfall: ", total_rainfall)

print("Average Monthly Rainfall: ", average_rainfall)

print("Month with highest rainfall:", MONTH_NAMES[highest_rainfall_index])

print("Month with lowest_rainfall:", MONTH_NAMES[lowest_rainfall_index])

main()

##Wondering if you can show all 3 sort methods

##thank you!

Solutions

Expert Solution

Here is the solution,

i have provided all the three function with names bubble_sort(), insertion_sort() and selection() and have used insertion_sort() for sorting. All the functions are just above the main() function.

#your code starts here
MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

NUMBER_OF_MONTHS = 12
def get_total_rainfall(months, months_len):

"""

Module Name: get_total_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Iterates over the array and calculates the total rainfall

"""

# Python Shortcut:

#total_rainfall = sum(months)

total_rainfall = 0

# Iterate of the array of months to accumulate the total.

for index in range(0, months_len):

total_rainfall += months[index]

return total_rainfall

def get_average_monthly_rainfall(months, months_len):

"""

Module Name: get_average_monthly_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Calculates and returns the average monthly rainfall based on the 'months' array

"""

# Average is calculated by dividing the total by the number of elements.

return get_total_rainfall(months, months_len) / months_len

def get_month_with_highest_rainfall(months, months_len):

"""

Module Name: get_month_with_highest_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Finds and returns the month with the highest rainfall.

Returns the first month if more than one month qualify as 'highest'

"""

# Sequentially search the array to find the maximum

max_index = 0

for index in range(1, months_len):

if months[max_index] < months[index]:

max_index = index

return max_index

def get_month_with_lowest_rainfall(months, months_len):

"""

Module Name: get_month_with_highest_rainfall

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: Finds and returns the month with the lowest rainfall.

Returns the first month if more than one month qualify as 'lowest'

"""

# Sequentially search the array to find the minimum

min_index = 0

for index in range(1, months_len):

if months[min_index] > months[index]:

min_index = index

return min_index


def bubble_sort(months, months_len):
"""
Module Name: bubble sort

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: sorting the array of months

Returns sorted List
  
"""
# iterate through the elements till months_len - 1
for i in range(months_len-1):
# iterate through the elements till months_len - i - 1
for j in range(months_len-i-1):
# comparing each bubble elements (index j with index j+1)
if months[j] > months[j+1] :
# swapping values
months[j], months[j+1] = months[j+1], months[j]
# return months
return months

def selection_sort(months, months_len):
"""
Module Name: selection sort

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: sorting the array of months

Returns sorted List
  
"""
# iterate thorugh all months
for i in range(months_len):
# assign i to Minimum_Rain
Minimum_Rain = i
# iterate through i + 1 till end.
for j in range(i+1, months_len):
# find the index with minimum value
if months[Minimum_Rain] > months[j]:
Minimum_Rain = j
#swap the value of minimum index value with index 'i'
months[i], months[Minimum_Rain] = months[Minimum_Rain], months[i]
# return months
return months


def insertion_sort(months, months_len):
"""
Module Name: insertion sort

Parameters: int [] -> months: an array of integers representing the rainfall in each month

int -> months_len: the length of the 'months' parameter array

Description: sorting the array of months

Returns sorted List
  
"""
# iterate thorugh all months, start from second position
for i in range(1,months_len):
# assign value at index i to rainfall element
rainfall_element = months[i]
# assign j the previous index
j = i-1
# loop and shift all elements one place to right that are greater than rainfall_element
while j >=0 and rainfall_element < months[j] :
# shifting of elements
months[j+1] = months[j]
# move j backward by decrementing
j -= 1
# placing rainfall_element to it's appropriate position
months[j+1] = rainfall_element
# return months
return months


def main():

"""

Module Name: main

Parameters: None

Description: Program entry point. Provides the program flow of execution

"""

months = [0.0] * NUMBER_OF_MONTHS

# Input

# Prompt the user to collect the rainfall for each month of the year
  
for index in range(0, NUMBER_OF_MONTHS):

print("Please enter the rainfall for month [", MONTH_NAMES[index], "]")

rainfall = float(input())

months[index] = rainfall

# Processing

# Calculate the attributes per the assignment

total_rainfall = get_total_rainfall(months, NUMBER_OF_MONTHS)

average_rainfall = get_average_monthly_rainfall(months, NUMBER_OF_MONTHS)
  
highest_rainfall_index = get_month_with_highest_rainfall(months, NUMBER_OF_MONTHS)

lowest_rainfall_index = get_month_with_lowest_rainfall(months, NUMBER_OF_MONTHS)


#bubble sort
insertion_sort(months, NUMBER_OF_MONTHS)

# Output

# Display the results

print("Total Rainfall: ", total_rainfall)

print("Average Monthly Rainfall: ", average_rainfall)

print("Month with highest rainfall:", MONTH_NAMES[highest_rainfall_index])

print("Month with lowest_rainfall:", MONTH_NAMES[lowest_rainfall_index])

print("Monthly Rainfall Sorted (ascending):")
for i in months:
print(i)
main()
# code ends here

here is the sample output:-

here are the screenshot of all the three functions:-

Thank You.


Related Solutions

Design a program that lets the user enter the total rainfall for each of 12 months...
Design a program that lets the user enter the total rainfall for each of 12 months into an array. 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. Requirements You must use the given function prototypes. You can add more functions if you like. def get_total_rainfall(months, months_len): def get_average_monthly_rainfall(months, months_len): def get_month_with_highest_rainfall(months, months_len): def get_month_with_lowest_rainfall(months, months_len): Design Considerations Use a global parallel array of...
Design a program that lets the user enter the total rainfall for each of 12 months...
Design a program that lets the user enter the total rainfall for each of 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts. This is my Pthon 3.8.5 code so far: #Determine Rainfall Program for a 12 month Period #It should calculate and output the "total yearly rainfall, avg monthly rainfall, the months with highest and lowest amounts #list of...
Design a program that lets the user enter the total rainfall for each of 12 months...
Design a program that lets the user enter the total rainfall for each of 12 months into an array. 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. Design a program to solve Chapter 8 Programming Exercise 3 (Rainfall Statistics) in your textbook. Create parallel arrays for the month names and rainfall amounts for each month. Use month names (i.e. January, February, March, etc.)...
In Python: Design a program that lets the user enter the total rainfall for each of...
In Python: Design a program that lets the user 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, the months with the highest and lowest amounts. However, to start, do not ask the user for input. Instead, hard code the monthly rainfalls as a list in your program, and then use the list to determine how to conduct the processing...
In python please design a program that lets the user enter the total rainfall for each...
In python please design a program that lets the user enter the total rainfall for each of the last 10 years into an array. The program should calculate and display the total rainfall for the decade, the average yearly rainfall, and the years with the highest and lowest amounts. Should have Indentation Comments Good Variables initializations Good questions asking for the parameters of the code Output display with explanation Screenshot
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
DESIGN A FLOWCHART IN FLOWGORITHM Rainfall Statistics Design a program that lets the user enter the...
DESIGN A FLOWCHART IN FLOWGORITHM Rainfall Statistics Design a program that lets the user enter the total rainfall for each of 12 months into an array. 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. PLEASE AND THANK YOU
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Program on Visual Basic, VBA Create an application that lets the user enter a number of...
Program on Visual Basic, VBA Create an application that lets the user enter a number of seconds and produces output according to the following criteria: There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT