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 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
// 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:
