Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The premise of this lab is the same as Lab #2, and you will be mainly refactoring code in that lab to meet the following

The premise of this lab is the same as Lab #2, and you will be mainly refactoring code in that lab to meet the following objectives. Show me your work after each objective is met.

All of the requirements for Lab #2 must still be met.

Refactor the code so that you use a struct (call it City) to store and access the name and population for each city.

Refactor the code so that as the city names and populations are read in, they are stored into an array of City records.

Refactor the code so that it contains the following two functions:

readCities This function will take as parameters your array of City records, a numCities variable (which will keep track of the number of cities read, and a file object. The job of this function is to read in all of the city names and populations from the file, storing them into your array of City records

printPopulations This function will take as parameters your array of City records, a numCities variable, and the number of people that each asterisk should represent. It will then print the city populations in the same format as before (i.e. with asterisks representing the number of people in each city).

Change your readCities function so that the cities are stored in alphabetical order by city name as they are read in (i.e. dont read them all in and then sort them after they are all stored. Reflect back to the strategy and code we implemented in the Array Practice in-class lab (problem #9) to help accomplish this.

(Optional 2 point bonus if you show both solutions) What would need to be changed in step 5) so that they were sorted by population instead of city name?

Lets practice a bit with arrays and common operations on arrays by completing the following tasks:

1. In the main function, declare two integer arrays called

scores

and

tallies

. These arrays should be

capable of holding 20 integers. You should use the same element counter variable for each array.

2. Add code to read 8 numbers from a file and store the numbers in the

scores

array. The test input file

should contain the numbers 11, 33, 55, 44, 22, 66, 88, 77. You may choose to add a sentinel to the end

of the file for read-in purposes, or simply read from the file until there are no more numbers to read in.

3. Add code that simply displays all of the numbers in the array on one line. Your test output should

appear as follows.

Scores: 11 33 55 44 22 66 88 77

4. Add code that uses a

for

loop to copy all 8 values of

scores

to

tallies

but in

reverse order

.

Then add code to print out both arrays side-by-side as depicted below.

Scores : 11 33 55 44 22 66 88 77

Tallies: 77 88 66 22 44 55 33 11

5.

Add code to find the index of the largest number and index of the smallest number in

scores

. Print out

both the index and the value of each number. In the test data, print as depicted below.

Scores Low: 11 at index 0

Scores High: 88 at index 6

6.

Add code to find the average of the numbers in

scores

and print that out as depicted below.

Scores: 49.5 average

7. Add code to reverse the order of the

scores

array without using the

tallies

array and without

declaring any other arrays (hint-use a

temp

variable and swap first and last elements, second and

second-to-last elements, etc.). Then add code to print out both arrays side-by-side as depicted below.

Scores : 77 88 66 22 44 55 33 11

Tallies: 77 88 66 22 44 55 33 11

8.

Add code to ask the user for a number to search. Search the

scores

array for that number. If found,

print out as depicted in the first line below. If not found, print out as depicted in the second line.

Scores: 44 is in the array

Scores: 99 is not in the array

9.

Create a new array. Write code that allows the user to type in numbers and store the numbers in

the array

in sorted order

#include

#include

using namespace std;

void printArray(int arr[], int numItems, string label);

int main(){

///////////////////////////////////////////////////////////////////////////

// Step 1

///////////////////////////////////////////////////////////////////////////

const int MAX_SCORES = 20;

int scores[MAX_SCORES];

int tallies[MAX_SCORES];

int numScores = 0;

int numTallies = 0;

int tmpScore;

///////////////////////////////////////////////////////////////////////////

// Step 2

///////////////////////////////////////////////////////////////////////////

ifstream inFile("scores.txt");

if (!inFile) {

cout << "Input file could not be opened. Exiting..." << endl;

return 1;

}

while (inFile >> tmpScore) {

scores[numScores] = tmpScore;

numScores++;

}

///////////////////////////////////////////////////////////////////////////

// Step 3

///////////////////////////////////////////////////////////////////////////

cout << "Step 3:" << endl;

printArray(scores, numScores, "Scores");

///////////////////////////////////////////////////////////////////////////

// Step 4

///////////////////////////////////////////////////////////////////////////

cout << endl << "Step 4:" << endl;

for (int i = 0; i < numScores; i++) {

tallies[i] = scores[numScores - 1 - i];

}

numTallies = numScores;

printArray(scores, numScores, "Scores");

printArray(tallies, numTallies, "Tallies");

///////////////////////////////////////////////////////////////////////////

// Step 5

///////////////////////////////////////////////////////////////////////////

// **Assumption** the scores array has at least 1 element

cout << endl << "Step 5:" << endl;

int smallestIdx = 0;

int biggestIdx = 0;

for (int i = 1; i < numScores; i++) {

if (scores[i] > scores[biggestIdx]) {

biggestIdx = i;

}

if (scores[i] < scores[smallestIdx]) {

smallestIdx = i;

}

}

cout << "Scores High: " << scores[biggestIdx] << " at index " << biggestIdx << endl;

cout << "Scores Low: " << scores[smallestIdx] << " at index " << smallestIdx << endl;

///////////////////////////////////////////////////////////////////////////

// Step 6

///////////////////////////////////////////////////////////////////////////

// **Assumption** the scores array has at least 1 element

cout << endl << "Step 6:" << endl;

double sum = 0;

double average;

for (int i = 0; i < numScores; i++) {

sum += scores[i];

}

average = sum / 0;

cout << "Average score is " << average << endl;

///////////////////////////////////////////////////////////////////////////

// Step 7

///////////////////////////////////////////////////////////////////////////

cout << endl << "Step 7:" << endl;

for (int i = 0; i < numScores / 2; i++) {

tmpScore = scores[i];

scores[i] = scores[numScores - 1 - i];

scores[numScores - 1 - i] = tmpScore;

}

printArray(scores, numScores, "Scores");

///////////////////////////////////////////////////////////////////////////

// Step 8

///////////////////////////////////////////////////////////////////////////

/*cout << endl << "Step 8:" << endl;

int target;

bool found = false;

cout << "Please enter a number for which to search: ";

cin >> target;

for (int i = 0; i < numScores; i++) {

if (scores[i] == target) {

cout << target << " is in the array." << endl;

found = true;

break;

}

}

if (found == false) {

cout << target << " is not in the array." << endl;

}

*/

///////////////////////////////////////////////////////////////////////////

// Step 9

///////////////////////////////////////////////////////////////////////////

cout << endl << "Step 9:" << endl;

numScores = 0;

int insertLoc = 0;

cout << "Please enter a score: ";

cin >> tmpScore;

while (tmpScore >= 0 && numScores < MAX_SCORES) {

//Find where the new element needs to go

insertLoc = 0;

while (insertLoc < numScores && tmpScore > scores[insertLoc]) {

insertLoc++;

}

//Make room for it

for (int i = numScores; i > insertLoc; i--) {

scores[i] = scores[i - 1];

}

//Insert it

scores[insertLoc] = tmpScore;

//Increase the number of elements

numScores++;

printArray(scores, numScores, "Scores");

cout << "Please enter a score: ";

cin >> tmpScore;

}

return 0;

}

void printArray(int arr[], int numItems, string label) {

cout << label << ":";

for (int i = 0; i < numItems; i++) {

cout << " " << arr[i];

}

cout << endl;

}

The objective of this lab is compare the populations of various cities that lie in between Toledo and Dayton on I-75. Write a program that produces a bar illustrating the populations. The program should read the name of the city and its population from a file. Have the program continue this process until the end of file is reached. For each city, your program should display the name of the city and a bar consisting of one asterisk for every 2,000 people. For example, if a citys population is between 18,000 and 19,999, 9 asterisks should appear in the bar. The data can be found in the pop.txt file located on Canvas.

For a hint on how to read data until the end of file is reached, look at the notes on repetition structures that we covered in class. Also look at the same class notes to review how to print out a certain number of asterisks to the screen.

The output should be formatted similar to the following:

City Populations

-----------------------------------------

Perrysburg **********

Bowling Green ***************

Findlay ********************

.

.

.

Huber Heights ******************

-----------------------------------------

KEY: (*) -> 2000 people

Once finished with the above task, modify your program to allow the user to type in how many people each asterisk represents (for example, typing 5000 would make the bar for Findlay 8 asterisks long instead of 20).

Finally, add code to your program to track the two cities in the file with the highest populations and list the results after the previously computed results. You can assume that no two cities have the same population.

data:

Perrysburg

21482

Bowling Green

31820

Findlay

41321

Bluffton

4144

Lima

37149

Wapakoneta

9782

Sidney

20614

Piqua

20987

Troy

25865

Tipp City

9721

Huber Heights

37986

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_2

Step: 3

blur-text-image_3

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

Students also viewed these Databases questions

Question

Which telepsychology is being used for which disorder?

Answered: 1 week ago