Question

In: Computer Science

c++ 1 city.h #pragma once #ifndef CITY_H #define CITY_H #include<string> #include<iostream> #include<utility> using namespace std; class...

c++

1 city.h

#pragma once

#ifndef CITY_H

#define CITY_H

#include<string>

#include<iostream>

#include<utility>

using namespace std;

class City {

public:

City(string nm, unsigned int pop) {

namePopulationPair = make_pair(nm, pop);

}

bool operator<(const City &city)const {

if (this->getName() < city.getName()) {

return true;

}

else {

return false;

}

}

void setName(string name) { namePopulationPair.first = name; }

void setPopulation(unsigned int population)

{

namePopulationPair.second = population;

}

string getName() const { return namePopulationPair.first; }

unsigned int getPopulation() const { return namePopulationPair.second; }

virtual void printInfo() const {

cout << getName() << ": " << getPopulation() << endl;

}

protected:

pair<string, unsigned int> namePopulationPair;

};

#endif

Use city.h from the previous lab without any modifications.

2 main.cpp

In main.cpp do the following step by step:

1. Globally define array cityArray[] consisting of cities with the following details:

(a) Los Angeles with population of 4 million

(b) San Diego with population of 1.5 million

(c) San Francisco with population of 900 thousand

(d) Sacramento with population of 500 thousand

(e) Stockton with the population of 300 thousand

(f) Redding with the population of 90 thousand

(g) Las Vegas with the population of 700 thousand

(h) Reno with the population of 300 thousand

(i) Portland with the population of 700 thousand

(j) Seattle with the population of 750 thousand

(k) Eugene with the population of 200 thousand

2. Globally define a vector of City objects, without initial values. Call it cityVector (1 points).

3. Pass vectors to these functions as reference, and define them as constant if the functions are not

allowed to modify them.

(a) Define function void initVector(...) that receives a vector of City objects, an array

of elements of type City as a second input, and an integer as its third input. The third input

represents the number of elements in the input array. Initialize the input queue with the elements

existing in the input array (2 points).

(b) Define function void printCityVector(...) that receives a vector of City objects

as input and prints the elements within the vector. Hint: You can use range-based for loops (2

points).

(c) Define function int mergeCityVector(...) that receives a vector of City objects as

input, along with three integers as indexes that represent the lower bound, the division point of

the vector into two halves, and the upper bound within the vector. It merges the two halves of

the vector (assuming they are sorted) according to the city populations (5 points).

(d) Define function void cityMergeSort(...) that receives a vector of City objects as

input, along with two integers as indexes that represent the lower and upper boundaries within

the vector. It does merge sort on the vector of City objects according to the city populations

(by invoking the mergeCityVector() function on sorted vectors (5 points).

In main() function do the following step by step, using the functions defined above:

(i) Initialize cityVector according to array cityArray[] using the function defined above (1

points).

(ii) Print out the entries of cityVector, using the appropriate function defined above (1 points).

(iii) Do merge sort on cityVector and print out the updated vector. (1 points).

The output of the program may look like the following:

Initializing cityVector with cityArray[]:

Los Angeles: 4000000

San Diego: 1500000

San Francisco: 900000

Sacramento: 500000

Stockton: 300000

Redding: 90000

Las Vegas: 700000

Reno: 300000

Portland: 700000

Seattle: 750000

Eugene: 200000

Merge sort on cityVector:

Redding: 90000

Eugene: 200000

Stockton: 300000

Reno: 300000

Sacramento: 500000

Las Vegas: 700000

Portland: 700000

Seattle: 750000

San Francisco: 900000

San Diego: 1500000

Los Angeles: 4000000

2

Solutions

Expert Solution

// city.h
#pragma once

#ifndef CITY_H

#define CITY_H

#include<string>
#include<iostream>
#include<utility>

using namespace std;

class City {

public:

City(string nm, unsigned int pop) {

namePopulationPair = make_pair(nm, pop);

}

bool operator<(const City &city)const {

if (this->getName() < city.getName()) {

return true;

}

else {

return false;

}

}

void setName(string name) { namePopulationPair.first = name; }

void setPopulation(unsigned int population)

{

namePopulationPair.second = population;

}

string getName() const { return namePopulationPair.first; }

unsigned int getPopulation() const { return namePopulationPair.second; }

virtual void printInfo() const {

cout << getName() << ": " << getPopulation() << endl;

}

protected:

pair<string, unsigned int> namePopulationPair;

};

#endif

//end of city.h

// main.cpp: C++ program to implement merge sort on the vector of City objects
#include <iostream>
#include <vector>
#include "city.h"
using namespace std;

// define a city array
City cityArray[] = {City("Los Angeles",4000000), City("San Diego",1500000), City("San Francisco",900000), City("Sacramento",500000), City("Stockton",300000), City("Redding",90000), City("Las Vegas",700000), City("Reno",300000), City("Portland",700000), City("Seattle",750000), City("Eugene",200000)};
// declare an empty city vector
vector<City> cityVector ;

// function declaration
void initVector(vector<City> &city, City cityArray[], int size);
void printCityVector(const vector<City> &city);
int mergeCityVector(vector<City> &city, int low, int mid, int high);
void cityMergeSort(vector<City> &city, int low, int high);

int main()
{
cout<<"Initializing cityVector with cityArray[]:"<<endl;
initVector(cityVector,cityArray,11);
printCityVector(cityVector);
cout<<"Merge sort on cityVector:"<<endl;
cityMergeSort(cityVector,0,cityVector.size()-1);
printCityVector(cityVector);
return 0;
}

// function to initialize the vector city to the contents of the cityArray
// It receives a vector of City objects, an array of elements of type City as a second input, and an integer as its third input
void initVector(vector<City> &city, City cityArray[], int size)
{
// loop over the array, inserting the ith record of cityArray to end of city vector
for(int i=0;i<size;i++)
{
city.push_back(cityArray[i]);
}
}

// function to display the city in vector city
// receives a vector of City objects as input and prints the elements within the vector
void printCityVector(const vector<City> &city)
{
// loop over the city vector
for(size_t i=0;i<city.size();i++)
city[i].printInfo(); // call printInfo to display the city name and population
}

/*
function that receives a vector of City objects as
input, along with three integers as indexes that represent the lower bound, the division point of
the vector into two halves, and the upper bound within the vector. It merges the two halves of
the vector (assuming they are sorted) according to the city populations
*/
int mergeCityVector(vector<City> &city, int low, int mid, int high)
{
int i,j,k;
int n1 = mid-low+1; // number of elements in first half of vector
int n2 = high-mid; // number of elements in second half of vector

vector<City> city1, city2; // create 2 temporary city vectors

// insert the contents of two halves of input city vector to city1 and city2 respectively
for(i=0;i<n1;i++)
city1.push_back(city[low+i]);
for(i=0;i<n2;i++)
city2.push_back(city[mid+1+i]);

i=0;
j=0;
k = low;

// loop to merge in 2 sorted vector into the city vector in sorted order
for(;i<n1 && j<n2;k++)
{
if(city1[i].getPopulation() <= city2[j].getPopulation())
{
city[k] = city1[i];
i++;
}else
{
city[k] = city2[j];
j++;
}
}

// if elements in city1 are left
for(;i<n1;i++,k++)
city[k] = city1[i];

// if elements in city2 are left
for(;j<n2;j++,k++)
city[k] = city2[j];


return 1;
}

/*
function that receives a vector of City objects as
input, along with two integers as indexes that represent the lower and upper boundaries within
the vector. It does merge sort on the vector of City objects according to the city populations
*/
void cityMergeSort(vector<City> &city, int low, int high)
{
// if low < high
if(low < high)
{
int mid = (low+high)/2; // get the mid point of division
cityMergeSort(city,low,mid); // call cityMergeSort on sub-vector between low and mid
cityMergeSort(city,mid+1,high); // call cityMergeSort on sub-vector between mid+1 and high
mergeCityVector(city,low,mid,high); // merge the 2 halves
}
}

//end of main.cpp

Output:


Related Solutions

Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x)...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator<<(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator/(int factor) { double half = amount / factor; return hafl; } ostream& operator<<(ostream& o, TuitionBill) { o << t.student << " Tuition:...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT