In: Computer Science
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!
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.