Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 8-2: Using a Bubble Sort In this lab, you complete a C++ program that uses an array to store data for the village of

Lab 8-2: Using a Bubble Sort

In this lab, you complete a C++ program that uses an array to store data for the village of Marengo. 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.

1. Open the source code file named HouseholdSize.cpp using Notepad or the text editor of your choice.

2. Write the bubble sort.

3. Output the mean and median household size in Marengo.

4. Save this source code file in a directory of your choice, and then make that directory your working directory.

5. Compile the source code file HouseholdSize.cpp.

6. Execute the program with the following input and record the output: Household sizes: 4, 1, 2, 4, 3, 3, 2, 2, 2, 4, 5, 6

Here is the Householdsize.cpp file information:

// HouseholdSize.cpp - This program uses a bubble sort to arrange up to 300 household sizes in

// descending order and then prints the mean and median household size.

// Input: Interactive.

// Output: Mean and median household size.

#include

#include

using namespace std;

int main()

{

// Declare variables.

const int SIZE = 300; // Number of household sizes

int householdSizes[SIZE]; // Array used to store 300 household sizes

int x;

int limit = SIZE;

int householdSize = 0;

int pairsToCompare;

bool switchOccurred;

int temp;

double sum = 0;

double mean = 0;

int medianIndex = 0;

// Input household size

cout

cin >> householdSize;

// Fill an array with household sizes - the maximum households = 300

x = 0;

while(x

{

// Place value in array.

householdSizes[x] = householdSize;

// Calculate total of household sizes using the sum variable

x++; // Get ready for next input item.

cout

cin >> householdSize;

} // End of input loop.

// set the limit to x

// calulate the mean household size by dividing the sum by the limit

// Use one of your sort routines to sort the Array so that the house sizes are in ascending order

// Print the mean

// Set medianIndex = (limit-1)/2

// Print the median found at householdSizes[medianIndex]

return 0;

} // End of main function

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

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

More Books

Students also viewed these Programming questions

Question

=+b) What would the data values in such an indicator variable be?

Answered: 1 week ago

Question

Can partitioned join be used for r r.A s? Explain your answer

Answered: 1 week ago