Question

C++ i want Lab#3 done can u make clear code so I could understand it. Lab#2The...

C++ i want Lab#3 done can u make clear code so I could understand it.

Lab#2The 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,000people. For example, if a city’s 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.

Lab#3

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.

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

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

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

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

a. 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

b. 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).

5) Change your readCities function so that the cities are stored in alphabetical order by city name as they are read in (i.e. don’t 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.

6) (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?

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code is implemented in c++

You didn't mentioned Array Practice in-class lab (problem #9).

If you provide that i will update the code

but as of now we just reading after that sorting

sort is in-built function in c++

#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 50
struct City{
   string name;
   double population;
};
bool comp_name(City a,City b)
{
   return (a.name< b.name );
}
bool comp_pop(City a,City b)
{
   return (a.population< b.population );
}
void readCities(City city[],int &numCities){
   string name,pop;
   ifstream fin;
    fin.open("pop.txt");
    while(fin){
        getline(fin, name);
        getline(fin, pop);
       city[numCities].name=name;
       city[numCities].population=atof(pop.c_str());
        numCities++;
    }
    numCities--;
    sort(city,city+numCities,comp_name);
    fin.close();
}
void printPopulations(struct City city[],int numCities,int ast){
   int highest=city[0].population,highest1=city[0].population,highindex=0,highindex1=0;
   for(int i=0;i<numCities;i++){
       cout<<city[i].name<<" ";
       for(int j=0;j<floor(city[i].population/ast);j++){
           cout<<"*";
       }
       cout<<endl;
       if(city[i].population>highest){
           highindex1=highindex;
           highest1=highest;
           highindex=i;
           highest=city[i].population;
       }
    }
    cout<<"Top 2 population cities are:"<<endl;
    cout<<city[highindex].name<<" "<<city[highindex].population<<endl;
    cout<<city[highindex1].name<<" "<<city[highindex1].population<<endl;
}
int main(){
   string line;
   struct City city[MAXSIZE];
   int numCities=0,ast;
   readCities(city,numCities);
    cout<<"Enter how many people each asterisk represents: "<<endl;
    cin>>ast;
    printPopulations(city,numCities,ast);
}

Add a comment
Know the answer?
Add Answer to:
C++ i want Lab#3 done can u make clear code so I could understand it. Lab#2The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

  • I need help with the following code... Instructions: This lab builds on the skills from Lab...

    I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...

  • I've done all questions except question 6. how do I do this? please help. code needs...

    I've done all questions except question 6. how do I do this? please help. code needs to be java 1. You need to be able to open and read the text files that have been provided for the project. Write a Java program that reads the driver.txt file and displays each data value for each line from the file to the screen in the following format: Licence Number: Licence Class: First Name: Last Name: Address: Suburb: Postcode: Demerit Points: Licence...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an ...

    I just need an algorithm for this please! I have C++ code for it but I dont know how to creat an algorithm .. CSE 1311-Project 4 Part I: Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. You can also put the data into a file and read the information into the program. The data is as follows: 150 250 Anne Bob Ralph 305...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT