Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Unit 8: Using a Bubble Sort in Python Summary: In this lab, you will complete a Python program that uses a list to store data

Unit 8: Using a Bubble Sort in Python

Summary:

In this lab, you will complete a Python program that uses a list to store data for the village of Marengo.

The village of Marengo conducted a census and collected records that contain household data, including the number of occupants in each household. The exact number of household records has not yet been determined, but you know that Marengo has fewer than 300 households.

The program is described in Chapter 8, Exercise 5, in Programming Logic and Design. The program should allow the user to enter each household size and determine the mean and median household size in Marengo. The program should output the mean and median household size in Marengo. The file provided for this lab contains the necessary variable declarations and input statements. You need to write the code that sorts the household sizes in ascending order using a bubble sort, and then prints the mean and median household size in Marengo. Comments in the code tell you where to write your statements.

Instructions:

Make sure that the file HouseholdSize.py is selected and open.

Write the bubble sort.

Calculate the total of the household size.

Output the mean and median household size in Marengo.

Execute the program by clicking the "Run Code" button and the bottom of the screen.

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.

Assignment:

"""

HouseholdSize.py - This program uses a bubble sort to arrange household sizes

in descending order and then prints the mean and median

household size.

Input: Interactive.

Output: Mean and median household size.

"""

def sortArray(householdSizes):

n = len(householdSizes)

# Traverse through all array elements

for i in range(n):

# Last i elements are already in place

for j in range(0, n-i-1):

# traverse the array from 0 to n-i-1

# Swap if the element found is lesser

# than the next element

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

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

def displayArray(householdSizes):

n = len(householdSizes)

for i in range(n):

print("%d" %householdSizes[i], )

# Initialize variables.

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

numSizes = 0

total = 0.0

mean = 0.0

median = 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

mean = 0.0

n = len(householdSizes)

for i in range(n):

mean += householdSizes[i];

mean /= n;

print("The mean Household Size is %.3f" %mean)

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

sortArray(householdSizes)

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

print("The Household Sizes in descending order : ")

displayArray(householdSizes)

# Find the median

if n % 2 == 1:

middle = int(n/2);

median = householdSizes[middle]

print("The median household size is %d" %median)

else:

middle = int(n/2)

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

print("The median household size is %.1f" %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

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions

Question

understand the meaning of the terms discipline and grievance

Answered: 1 week ago