Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can I get help with this program please. Instructions Make sure that the file HouseholdSize.py is selected and open. Write the bubble sort. Calculate the

Can I get help with this program please.

Instructions

  1. Make sure that the file HouseholdSize.py is selected and open.
  2. Write the bubble sort.
  3. Calculate the total of the household size.
  4. Output the mean and median household size in Marengo.
  5. Execute the program by clicking the Run button and the bottom of the screen.
  6. Enter the following input, and ensure the output is correct. Household sizes: 4, 1, 2, 4, 3, 3, 2, 2, 2, 4, 5, 6 followed by 999 to exit the program.

This is the code I have

# Initialize variables.

householdSizes = [] # Array used to store household sizes.

numSizes = 0

total = 0.0

mean = 0.0

median = 0.0

# Input household size

householdSizeString = input("Enter household size or 999 to quit: ")

householdSize = int(householdSizeString)

# This is the work done in the fillArray() function

while (householdSize != 999):

# Place value in array.

householdSizes.append(householdSize)

# Calculate total of household sizes

numSizes += 1 # We have one more house

householdSizeString = input("Enter household size or 999 to quit: ")

householdSize = int(householdSizeString)

# Find the mean

def findMean(householdSizes):

sum_val=0

#summing the values in householdSizes

for i in householdSizes:

sum_val+=i

#dividing by length of householdSizes to get the mean value

return sum_val/len(householdSizes)

# This is the work done in the sortArray() function

def sortArray(householdSizes):

#using bubble sort algorithm to sort householdSizes in ascending order

for i in range(len(householdSizes)):

for j in range(len(householdSizes)-1):

#comparing elements at indices j and j+1

if householdSizes[j]>householdSizes[j+1]:

#swapping elements at indices j and j+1

householdSizes[j],householdSizes[j+1]=householdSizes[j+1],householdSizes[j]

# This is the work done in the displayArray() function

def displayArray(householdSizes):

#printing all elements in householdSizes separated by comma and space

for i in range(len(householdSizes)):

print(householdSizes[i],end='')

#if this is not last item printing ', '

if i!=len(householdSizes)-1:

print(', ',end='')

print()

# Find the median

if len(householdSizes)%2!=0:

median=householdSizes[len(householdSizes)//2]

else:

median=(householdSizes[len(householdSizes)//2] + householdSizes[(len(householdSizes)//2) -1])/2

#displaying median

print('Median of household sizes:',median)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

When does a country become an exporter of a good? An importer?

Answered: 1 week ago