SlideShare a Scribd company logo
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 1 of 7
Homework #3
1. The objective of this homework:
To exercise the various basic C++ language constructs, file
streams, arrays, and strings. Additionally, you will
practice the best practices for placement of code into different
types of files.
2. Problem Description
You will build a program that will assist users in planning trips
in the United States by helping them determining
the distances between major cities as well as the estimate cost
of the trip. Furthermore, your program will
contemplate especial circumstances where detours are needed,
mainly due to weather related closures like those
that happen when you are traveling in the I-70 mountain
corridor from Denver to Los Angeles in winter.
In your program you will be using a distance grid provided by a
15x15 matrix. This matrix includes a starting point
city (source city) in the rows, a destination point (target city) in
the columns and each cell contains the distance
between source and target expressed in miles for 15 major cities
in the U.S. A fragment of such matrix is included
below. The data was obtained from
https://www.mapcrow.info/united_states.html.
Atlanta Boston Chicago … Washington DC
Atlanta 0 1505 944 871
Boston 1505 0 1366 634
Chicago 944 1366 0 956
…
Washington DC 871 634 956 0
For instance, the distance from Chicago to Washington DC is
956 miles, which comes from the cell formed by the
intersection of the row labeled Chicago and the column labeled
Washington DC. The complete distance matrix is
provided for you in a file.
3. Program
Your program will, using the distance matrix, help users to plan
trips, by providing with the travel distances, and
estimated fuel cost when traveling withing these 15 cities.
The basic application will present the following menu:
---------------------------------
Main Menu
------------------------------
1) Load Cities and Distances
2) Add Weather Detour
3) Distance Between Cities
4) Distance and Trip Cost
5) Average Trip Distance
6) Closest City
7) Farthest City
8) Closest Two Cities
9) Farthest Two Cities
99) EXIT
------------------------------
https://www.mapcrow.info/united_states.html
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 2 of 7
• Option (1): loads cities and distances, will load the database
from the provided files. This will be the first
step in the program. When other options (2 through 9) are
selected before loading the database, your
program should display a warning message to the user guiding
him/her to load the database first.
• Option (2): adds a weather detour will ask for a source city, a
destination city, and a detour distance which
will be added to the travel distance between source and
destination. Your program should also ask if the
detour is one way (source to target) or both ways (source to
target and target to source) as well.
• Option (3): asks the user for two city names and will display
the distance, expressed in miles between the
two cities. E.g., “The current distance between DENVER and
LOS ANGELES 1410 miles.”
• Option (4): asks for two city names, the average miles per
gallon (mpg) performance of the car, and the
average cost of the gas, to provide the information for the trip.
E.g., “The current distance between
DENVER and LOS ANGELES is 1410 miles. The trip would
cost $145.41 in a car that performs 32mpg and a
gas price of $3.30/gallon.”
• Option (5): asks for a city name and display the average
distance for a trip starting at that city. E.g., “From
DENVER the average trip distance is 1850.14 miles”.
• Option (6): asks for a city name and displays the closest city
to the given one. E.g., “The closest city to
DENVER is PHOENIX, 942 miles away.”
• Option (7): asks for a city name and displays the farthest city
to the given one. E.g., “The farthest city to
DENVER is BOSTON, 2838 miles away.”
• Option (8): asks for a city name and displays the two closest
cities to the given one. E.g., “The two closest
cities to DENVER are PHOENIX, 942 miles away and
DALLAS, 1064 miles away.”
• Option (9): asks for a city name and displays the farthest city
to the given one. E.g., “The two farthest
cities to DENVER are BOSTON, 2838 miles away and MIAMI,
2773 miles away.”
• Option (99): asks the user for confirmation and terminates the
program.
o The program should keep running until the user select to
terminate the program.
• All decimal numbers should be displayed with a precision of 2
decimal places.
• The following section provide more implementation details for
your program.
3.1. Implementation Details
3.1.1. Provided Database Files
• City names, and the corresponding index in the distance
matrix is provided on the file called cities.txt.
o This file has one row per city (total 15).
o Each row, has the formant <INDEX> <CITY_NAME>, e.g.,
“1 Atlanta”
o The index shows the number of row and column in the
distances file that correspond to the city.
▪ E.g., “1 Atlanta” represents that both row and column one in
the distance matrix
correspond to the city of Atlanta.
• The distance matrix is provided on the file called
distances.txt.
o The file contains 225 lines. Each line has one single value
that corresponds to a cell in the matrix.
o The first 15 rows correspond to the cells (destinations) for the
first city (e.g., Atlanta).
o The second 15 rows correspond to the cells (destinations) for
the second city (e.g., Boston).
o So on, so forth.
o city_distances.txt file, provides a human-readable matrix, just
for reference.
3.1.2. Source Files
• Place your main program in the main.cpp file.
• Place the functions (see section 3.1.4) declarations in
distances.h file and the definition of those in the
distances.cpp file.
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 3 of 7
3.1.3. Main Program
• Your program must use the functions (see section 3.1.4) as
much as possible.
o This improves maintainability, usability, and readability of
your code.
• Declare a global constant (NUMBER_CITIES) in your
program to set the number of cities in your data
(currently 15).
• Should declare the 2D-Array as well as the array for cities
within the main program (not global).
• All input data should be validated. E.g., a city name input by
the user should be in the database.
• All city names must be case-insensitive, i.e., Denver, denver,
DENVER and DENver should be treated as
the same.
o Hint: convert all city names to uppercase (use the toUppercase
function).
• Use the std::string to manipulate strings as much as possible.
3.1.4. Function Specification
Function Name loadCities
Description Loads an array with all the cities names from the
file cities.txt
Returns Does not return data.
Parameters 1) an array of strings where the city names will be
loaded into.
2) an integer indicating the number of cities to load.
Comments The number of cities would be defined in a constant
in your main program. Use it when calling
the function. See section 3.1.1 for file content description.
Hint 1: store the cities in the given order, i.e., Atlanta should be
the first element of your
array. Remember 0-indexing in C++.
Hint 2: use upper-case strings.
Function Name loadDistances
Description Loads a 2D-array (#cities×#cities matrix) with all
the distances from the file distances.txt
Returns Does not return data.
Parameters 1) a 2-D array of integers to load the distance matrix
into.
2) an integer indicating the number of cities to load.
Comments The indexing and the corresponding city name will
be given by the cities array (loadCities
function). See section 3.1.1 for file content description.
Use nested loops to load the arrays as you read the lines.
Function Name getCityIndex
Description Returns the array-index for a given city name.
Returns Returns an integer between 0 and NUMBER_CITIES-1
Returns -1 if the city is not in the list
Parameters 1) a string with the city name
2) an integer indicating the number of cities available.
Comments Hint: City name argument may have any case.
Function Name toUppercase
Description Converts a string to uppercase
Returns Returns a new string corresponding to the string
argument into uppercase
Parameters 1) a string (should not be modified)
Comments
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 4 of 7
Function Name addDetour
Description Adds a detour due to weather between two cities.
Returns Does not return data.
Parameters 1) a 2-D array of integers to load the distance matrix
into.
2) an integer indicating the number of cities.
3) the source city name
4) the target city name
Comments Use getCityIndex function.
Validate that the index is not -1.
Function Name getTripCost
Description Computes the estimated cost in dollars for a trip.
Returns Returns a double precision floating point number with
the cost value
Parameters 1) an integer indicating the distance in miles
2) an integer indicating the miles per gallon (mpg)
3) a double indicating the gas cost per gallon
Comments �������� / ��� × �����������
Function Name getTripAverageDistance
Description Computes the average distance of trip from a
starting city.
Returns Returns a double precision floating point number with
the average
Parameters 1) a 2-D array of integers with the distance matrix.
2) an integer indicating the number of cities.
3) the source city name
Comments The source city should NOT be considered as a
target when computing the average (i.e., do
not count Denver to Denver trip).
Use a for loop and recall the continue statement.
Function Name getClosestCity
Description Computes the closest city and returns the string
“<CLOSEST_CITY>, XXX miles away”
Returns Returns the described string.
Parameters 1) a 2-D array of integers with the distance matrix.
2) an integer indicating the number of cities.
3) the source city name
Comments The source city should not be considered as a target
when computing the average (i.e., do
not count Denver to Denver).
Function Name getFarthestCity
Description Computes the farthest city and returns the string
“<FARTHEST_CITY>, XXX miles away”
Returns Returns the described string.
Parameters 1) a 2-D array of integers with the distance matrix.
2) an integer indicating the number of cities.
3) the source city name
Comments The source city should not be considered as a target
when computing the average (i.e., do
not count Denver to Denver).
Function Name getClosestTwoCities
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 5 of 7
Description Computes the two closest cities and returns the
string “<CITY1>, XXX miles away and
<CITY2>, YYY miles away”
Returns Returns the described string.
Parameters 1) a 2-D array of integers with the distance matrix.
2) an integer indicating the number of cities.
3) the source city name
Comments The source city should not be considered as a target
when computing the average (i.e., do
not count Denver to Denver).
Function Name getFarthestTwoCities
Description Computes the farthest city and returns the string
“<CITY1>, XXX miles away and
<CITY2>, YYY miles away”
Returns Returns the described string.
Parameters 1) a 2-D array of integers with the distance matrix.
2) an integer indicating the number of cities.
3) the source city name
Comments The source city should not be considered as a target
when computing the average (i.e., do
not count Denver to Denver).
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 6 of 7
4. Extra Credit (10 marks)
Add an option 10 which will let the user search cities which
names can be formed with the letters on a given string.
This option reads the text and calls the function
displayMatchingDistances.
Function Name displayMatchingDistances
Description Prints to console the distances for cities which
name can be formed with the sequence of
characters.
Returns Does not return.
Parameters 1) a 2-D array of integers with the distance matrix.
2) an array of string with the city names.
3) an integer indicating the number of cities.
4) the source string
Comments See algorithm below.
The algorithm for this option should be as follows:
1. Ask the user for a word or phrase.
2. Convert the string to uppercase.
3. Call displayMatchingDistances
The algorithm for displayMatchingDistances should be as
follows:
1. Declare charCount, an integer array to store the count of
letters (26 letters)
a. Initialize all the elements in charCount to 0
2. Traverse the characters in the phrase to count the character
occurrence.
a. Ignore any character that is not a letter (A-Z).
b. For each character, increment by one the count for that letter
in the
charCount array
i. HINT 1: use the ascii code to MAP the character with the
array
index, e.g., A → 0.
ii. HINT 2: a char can be interpreted as and int (e.g., ‘A’+ 0 →
65)
3. For each city, build a similar array counting the letters in the
city’s name.
a. Check if the City Matches:
i. A city matches when all needed letters for the city’s name
are in the letters included in the phrase.
ii. E.g., for DENVER you need 1xD, 2xE, 1xN, 1xV and 1xR
iii. So, DENVER will match NEVER DIE, but will not match
EVERGREEN
b. If the City matches:
i. Display the distances to all other cities:
<MATCHED CITY> - <Destination> - <Distance>
Example:
Input Phrase: NEVER DIE NEW YORK
MATCHES: DENVER
-------------------------------
DENVER – ATLANTA – 1945
DENVER – BOSTON – 2838
...
MATCHES: NEW YORK
-------------------------------
NEW YORK – ATLANTA – 1200
NEW YORK – BOSTON – 306
...
CSCI2312 – Object Oriented Programming Section 003
Homework 3 Fall 2022 Page 7 of 7
5. Testing
- Test your program.
- Make sure that the changes for instance by adding a detour are
reflected in the following option.
o Example:
▪ Get the distance between Denver and Los Angeles
▪ Add a detour of 100miles from Denver to Los Angeles
▪ Get the distance again and compare.
- Test inputs, e.g., type a city name that is not included. Your
program should show an error, but your
program needs to keep running.
6. Deliverables / Submission
1. Code should follow the discussed guidelines regarding
naming conventions, coding style and comments.
a. Comment your code. No need to comment every single line of
code but add comments explaining
what you are doing.
2. Develop your code in CLion. You will submit the entire
CLion Project.
3. Create a makefile to compile and run your code in CSE.
Capture a screenshot of compilation output and
program running (see 4.c below).
4. You need to submit a total of three files to Canvas
Assignment page:
a. hwk3.zip
i. A compressed file that includes all CLion files for the
homework.
ii. Should contain at least main.cpp, distances.h, distances.cpp,
cities.txt and
distances.txt.
iii. Include the Readme.md file in the CLion Project.
iv. Note in the Readme if you are completing or not the Extra
Credit.
b. makefile
i. The makefile you used to compile the program in CSE Grid.
c. hwk3.png or hwk3.jpg
i. a screenshot of your program running on CSE Grid.
ii. Include the result of option (3) between Denver and Boston.
5. Submit all your files in one single submission, otherwise you
will be overwriting your previous submission.
1. The objective of this homework:2. Problem Description3.
Program3.1. Implementation Details3.1.1. Provided Database
Files3.1.2. Source Files3.1.3. Main Program3.1.4. Function
Specification
CSCI2312 – Object Oriented Programming  Section 003 Homewo

More Related Content

PPTX
ExploringPrimsAlgorithmforMinimumSpanningTreesinC.pptx
DOCX
ECS 40 Program #1 (50 points, my time 2.5 hours) .docx
DOCX
ECS 40 Program #3 (50 points, my time 1.5 hours) Spring 2015 .docx
PPTX
CPP Homework Help
PPTX
City focus: A web-based interactive 2D and 3D GIS application to find the bes...
PDF
IRJET- Comparison for Max-Flow Min-Cut Algorithms for Optimal Assignment Problem
PPTX
Computer Science Programming Assignment Help
PPTX
Computer Science Assignment Help
ExploringPrimsAlgorithmforMinimumSpanningTreesinC.pptx
ECS 40 Program #1 (50 points, my time 2.5 hours) .docx
ECS 40 Program #3 (50 points, my time 1.5 hours) Spring 2015 .docx
CPP Homework Help
City focus: A web-based interactive 2D and 3D GIS application to find the bes...
IRJET- Comparison for Max-Flow Min-Cut Algorithms for Optimal Assignment Problem
Computer Science Programming Assignment Help
Computer Science Assignment Help

Similar to CSCI2312 – Object Oriented Programming Section 003 Homewo (20)

PDF
Efficient call path detection for android os size of huge source code
PDF
EFFICIENT CALL PATH DETECTION FOR ANDROID-OS SIZE OF HUGE SOURCE CODE
PPT
Machine Learning Approach to Report Prioritization with an ...
PPTX
ThesisPresentation
PPTX
Reporting Summary Information of Spatial Datasets and Non-Compliance Issues U...
PDF
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
PPT
R Spatial Analysis using SP
PPTX
Computation Assignment Help
PDF
IRJET- Survey on Implementation of Graph Theory in Routing Protocols of Wired...
PPTX
Unit 2 part-2
PDF
Monzor, Carbon-R-a, and the end of the world
DOCX
Name _______________________________ Class time __________.docx
PDF
Description Of A Graph
PPT
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
PDF
Project Report
PPTX
Comparative Analysis of Distance Vector Routing & Link State Protocols
PDF
The Functional Programming Triad of Map, Filter and Fold
PDF
Map reduce
PDF
2004 map reduce simplied data processing on large clusters (mapreduce)
PDF
Lecture 1 mapreduce
Efficient call path detection for android os size of huge source code
EFFICIENT CALL PATH DETECTION FOR ANDROID-OS SIZE OF HUGE SOURCE CODE
Machine Learning Approach to Report Prioritization with an ...
ThesisPresentation
Reporting Summary Information of Spatial Datasets and Non-Compliance Issues U...
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
R Spatial Analysis using SP
Computation Assignment Help
IRJET- Survey on Implementation of Graph Theory in Routing Protocols of Wired...
Unit 2 part-2
Monzor, Carbon-R-a, and the end of the world
Name _______________________________ Class time __________.docx
Description Of A Graph
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
Project Report
Comparative Analysis of Distance Vector Routing & Link State Protocols
The Functional Programming Triad of Map, Filter and Fold
Map reduce
2004 map reduce simplied data processing on large clusters (mapreduce)
Lecture 1 mapreduce

More from simisterchristen (20)

DOCX
Reflection essay should be at least 350-400 words.Student resp.docx
DOCX
Reflection is no less than one page, but no more than two pages. (2..docx
DOCX
Reflecting on Personal Identity and Global CitizenshipReview the .docx
DOCX
Reflecting on Personal Identity and Global CitizenshipReview the.docx
DOCX
Reflecting on the movie we watched in class, 12 Angry Men, please ad.docx
DOCX
Reflect on your understanding of the relationship between thinking a.docx
DOCX
Reflect on your experiences during research processes and MLA style.docx
DOCX
Reflect on what you learned in regards to mission statements.1) Di.docx
DOCX
Reflect on the following for your 1-page journal reflection. As a ma.docx
DOCX
Reflect on what you have learned in this course.What future concer.docx
DOCX
Reflect on this semester as it is coming to an end.  Please summariz.docx
DOCX
Reflect on the University Personal Development. What impediments.docx
DOCX
Reflect on an experience when you interacted with someone from anoth.docx
DOCX
ReferencesAssignment Submit a reference list showing your r.docx
DOCX
Referenced from American Literature Since the Civil War. Create.docx
DOCX
Refer to the project from your local community or state that you des.docx
DOCX
Recruitment Methods  Please respond to the followingDevelop a b.docx
DOCX
Recommended Pages 5Style MLACitations Have a works cited page.docx
DOCX
Reducing Communication BarriersIdentify what techniques you can im.docx
DOCX
Red-green color blindness in humans is an example of __________..docx
Reflection essay should be at least 350-400 words.Student resp.docx
Reflection is no less than one page, but no more than two pages. (2..docx
Reflecting on Personal Identity and Global CitizenshipReview the .docx
Reflecting on Personal Identity and Global CitizenshipReview the.docx
Reflecting on the movie we watched in class, 12 Angry Men, please ad.docx
Reflect on your understanding of the relationship between thinking a.docx
Reflect on your experiences during research processes and MLA style.docx
Reflect on what you learned in regards to mission statements.1) Di.docx
Reflect on the following for your 1-page journal reflection. As a ma.docx
Reflect on what you have learned in this course.What future concer.docx
Reflect on this semester as it is coming to an end.  Please summariz.docx
Reflect on the University Personal Development. What impediments.docx
Reflect on an experience when you interacted with someone from anoth.docx
ReferencesAssignment Submit a reference list showing your r.docx
Referenced from American Literature Since the Civil War. Create.docx
Refer to the project from your local community or state that you des.docx
Recruitment Methods  Please respond to the followingDevelop a b.docx
Recommended Pages 5Style MLACitations Have a works cited page.docx
Reducing Communication BarriersIdentify what techniques you can im.docx
Red-green color blindness in humans is an example of __________..docx

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Institutional Correction lecture only . . .
GDM (1) (1).pptx small presentation for students
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

CSCI2312 – Object Oriented Programming Section 003 Homewo

  • 1. CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 1 of 7 Homework #3 1. The objective of this homework: To exercise the various basic C++ language constructs, file streams, arrays, and strings. Additionally, you will practice the best practices for placement of code into different types of files. 2. Problem Description You will build a program that will assist users in planning trips in the United States by helping them determining the distances between major cities as well as the estimate cost of the trip. Furthermore, your program will contemplate especial circumstances where detours are needed, mainly due to weather related closures like those that happen when you are traveling in the I-70 mountain corridor from Denver to Los Angeles in winter. In your program you will be using a distance grid provided by a 15x15 matrix. This matrix includes a starting point city (source city) in the rows, a destination point (target city) in the columns and each cell contains the distance between source and target expressed in miles for 15 major cities in the U.S. A fragment of such matrix is included below. The data was obtained from https://www.mapcrow.info/united_states.html. Atlanta Boston Chicago … Washington DC
  • 2. Atlanta 0 1505 944 871 Boston 1505 0 1366 634 Chicago 944 1366 0 956 … Washington DC 871 634 956 0 For instance, the distance from Chicago to Washington DC is 956 miles, which comes from the cell formed by the intersection of the row labeled Chicago and the column labeled Washington DC. The complete distance matrix is provided for you in a file. 3. Program Your program will, using the distance matrix, help users to plan trips, by providing with the travel distances, and estimated fuel cost when traveling withing these 15 cities. The basic application will present the following menu: --------------------------------- Main Menu ------------------------------ 1) Load Cities and Distances 2) Add Weather Detour 3) Distance Between Cities 4) Distance and Trip Cost 5) Average Trip Distance
  • 3. 6) Closest City 7) Farthest City 8) Closest Two Cities 9) Farthest Two Cities 99) EXIT ------------------------------ https://www.mapcrow.info/united_states.html CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 2 of 7 • Option (1): loads cities and distances, will load the database from the provided files. This will be the first step in the program. When other options (2 through 9) are selected before loading the database, your program should display a warning message to the user guiding him/her to load the database first. • Option (2): adds a weather detour will ask for a source city, a destination city, and a detour distance which will be added to the travel distance between source and destination. Your program should also ask if the detour is one way (source to target) or both ways (source to target and target to source) as well.
  • 4. • Option (3): asks the user for two city names and will display the distance, expressed in miles between the two cities. E.g., “The current distance between DENVER and LOS ANGELES 1410 miles.” • Option (4): asks for two city names, the average miles per gallon (mpg) performance of the car, and the average cost of the gas, to provide the information for the trip. E.g., “The current distance between DENVER and LOS ANGELES is 1410 miles. The trip would cost $145.41 in a car that performs 32mpg and a gas price of $3.30/gallon.” • Option (5): asks for a city name and display the average distance for a trip starting at that city. E.g., “From DENVER the average trip distance is 1850.14 miles”. • Option (6): asks for a city name and displays the closest city to the given one. E.g., “The closest city to DENVER is PHOENIX, 942 miles away.” • Option (7): asks for a city name and displays the farthest city to the given one. E.g., “The farthest city to DENVER is BOSTON, 2838 miles away.” • Option (8): asks for a city name and displays the two closest cities to the given one. E.g., “The two closest cities to DENVER are PHOENIX, 942 miles away and DALLAS, 1064 miles away.” • Option (9): asks for a city name and displays the farthest city to the given one. E.g., “The two farthest cities to DENVER are BOSTON, 2838 miles away and MIAMI, 2773 miles away.” • Option (99): asks the user for confirmation and terminates the
  • 5. program. o The program should keep running until the user select to terminate the program. • All decimal numbers should be displayed with a precision of 2 decimal places. • The following section provide more implementation details for your program. 3.1. Implementation Details 3.1.1. Provided Database Files • City names, and the corresponding index in the distance matrix is provided on the file called cities.txt. o This file has one row per city (total 15). o Each row, has the formant <INDEX> <CITY_NAME>, e.g., “1 Atlanta” o The index shows the number of row and column in the distances file that correspond to the city. ▪ E.g., “1 Atlanta” represents that both row and column one in the distance matrix correspond to the city of Atlanta. • The distance matrix is provided on the file called distances.txt. o The file contains 225 lines. Each line has one single value that corresponds to a cell in the matrix. o The first 15 rows correspond to the cells (destinations) for the first city (e.g., Atlanta). o The second 15 rows correspond to the cells (destinations) for the second city (e.g., Boston). o So on, so forth. o city_distances.txt file, provides a human-readable matrix, just
  • 6. for reference. 3.1.2. Source Files • Place your main program in the main.cpp file. • Place the functions (see section 3.1.4) declarations in distances.h file and the definition of those in the distances.cpp file. CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 3 of 7 3.1.3. Main Program • Your program must use the functions (see section 3.1.4) as much as possible. o This improves maintainability, usability, and readability of your code. • Declare a global constant (NUMBER_CITIES) in your program to set the number of cities in your data (currently 15). • Should declare the 2D-Array as well as the array for cities within the main program (not global). • All input data should be validated. E.g., a city name input by the user should be in the database. • All city names must be case-insensitive, i.e., Denver, denver, DENVER and DENver should be treated as the same.
  • 7. o Hint: convert all city names to uppercase (use the toUppercase function). • Use the std::string to manipulate strings as much as possible. 3.1.4. Function Specification Function Name loadCities Description Loads an array with all the cities names from the file cities.txt Returns Does not return data. Parameters 1) an array of strings where the city names will be loaded into. 2) an integer indicating the number of cities to load. Comments The number of cities would be defined in a constant in your main program. Use it when calling the function. See section 3.1.1 for file content description. Hint 1: store the cities in the given order, i.e., Atlanta should be the first element of your array. Remember 0-indexing in C++. Hint 2: use upper-case strings. Function Name loadDistances Description Loads a 2D-array (#cities×#cities matrix) with all the distances from the file distances.txt Returns Does not return data. Parameters 1) a 2-D array of integers to load the distance matrix into.
  • 8. 2) an integer indicating the number of cities to load. Comments The indexing and the corresponding city name will be given by the cities array (loadCities function). See section 3.1.1 for file content description. Use nested loops to load the arrays as you read the lines. Function Name getCityIndex Description Returns the array-index for a given city name. Returns Returns an integer between 0 and NUMBER_CITIES-1 Returns -1 if the city is not in the list Parameters 1) a string with the city name 2) an integer indicating the number of cities available. Comments Hint: City name argument may have any case. Function Name toUppercase Description Converts a string to uppercase Returns Returns a new string corresponding to the string argument into uppercase Parameters 1) a string (should not be modified) Comments
  • 9. CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 4 of 7 Function Name addDetour Description Adds a detour due to weather between two cities. Returns Does not return data. Parameters 1) a 2-D array of integers to load the distance matrix into. 2) an integer indicating the number of cities. 3) the source city name 4) the target city name Comments Use getCityIndex function. Validate that the index is not -1. Function Name getTripCost Description Computes the estimated cost in dollars for a trip. Returns Returns a double precision floating point number with the cost value Parameters 1) an integer indicating the distance in miles 2) an integer indicating the miles per gallon (mpg) 3) a double indicating the gas cost per gallon Comments �������� / ��� × ����������� Function Name getTripAverageDistance Description Computes the average distance of trip from a
  • 10. starting city. Returns Returns a double precision floating point number with the average Parameters 1) a 2-D array of integers with the distance matrix. 2) an integer indicating the number of cities. 3) the source city name Comments The source city should NOT be considered as a target when computing the average (i.e., do not count Denver to Denver trip). Use a for loop and recall the continue statement. Function Name getClosestCity Description Computes the closest city and returns the string “<CLOSEST_CITY>, XXX miles away” Returns Returns the described string. Parameters 1) a 2-D array of integers with the distance matrix. 2) an integer indicating the number of cities. 3) the source city name Comments The source city should not be considered as a target when computing the average (i.e., do not count Denver to Denver). Function Name getFarthestCity Description Computes the farthest city and returns the string “<FARTHEST_CITY>, XXX miles away” Returns Returns the described string.
  • 11. Parameters 1) a 2-D array of integers with the distance matrix. 2) an integer indicating the number of cities. 3) the source city name Comments The source city should not be considered as a target when computing the average (i.e., do not count Denver to Denver). Function Name getClosestTwoCities CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 5 of 7 Description Computes the two closest cities and returns the string “<CITY1>, XXX miles away and <CITY2>, YYY miles away” Returns Returns the described string. Parameters 1) a 2-D array of integers with the distance matrix. 2) an integer indicating the number of cities. 3) the source city name Comments The source city should not be considered as a target when computing the average (i.e., do not count Denver to Denver). Function Name getFarthestTwoCities Description Computes the farthest city and returns the string “<CITY1>, XXX miles away and
  • 12. <CITY2>, YYY miles away” Returns Returns the described string. Parameters 1) a 2-D array of integers with the distance matrix. 2) an integer indicating the number of cities. 3) the source city name Comments The source city should not be considered as a target when computing the average (i.e., do not count Denver to Denver). CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 6 of 7 4. Extra Credit (10 marks) Add an option 10 which will let the user search cities which names can be formed with the letters on a given string. This option reads the text and calls the function displayMatchingDistances. Function Name displayMatchingDistances Description Prints to console the distances for cities which name can be formed with the sequence of characters. Returns Does not return.
  • 13. Parameters 1) a 2-D array of integers with the distance matrix. 2) an array of string with the city names. 3) an integer indicating the number of cities. 4) the source string Comments See algorithm below. The algorithm for this option should be as follows: 1. Ask the user for a word or phrase. 2. Convert the string to uppercase. 3. Call displayMatchingDistances The algorithm for displayMatchingDistances should be as follows: 1. Declare charCount, an integer array to store the count of letters (26 letters) a. Initialize all the elements in charCount to 0 2. Traverse the characters in the phrase to count the character occurrence. a. Ignore any character that is not a letter (A-Z). b. For each character, increment by one the count for that letter in the charCount array i. HINT 1: use the ascii code to MAP the character with the array index, e.g., A → 0.
  • 14. ii. HINT 2: a char can be interpreted as and int (e.g., ‘A’+ 0 → 65) 3. For each city, build a similar array counting the letters in the city’s name. a. Check if the City Matches: i. A city matches when all needed letters for the city’s name are in the letters included in the phrase. ii. E.g., for DENVER you need 1xD, 2xE, 1xN, 1xV and 1xR iii. So, DENVER will match NEVER DIE, but will not match EVERGREEN b. If the City matches: i. Display the distances to all other cities: <MATCHED CITY> - <Destination> - <Distance> Example: Input Phrase: NEVER DIE NEW YORK MATCHES: DENVER ------------------------------- DENVER – ATLANTA – 1945 DENVER – BOSTON – 2838 ... MATCHES: NEW YORK -------------------------------
  • 15. NEW YORK – ATLANTA – 1200 NEW YORK – BOSTON – 306 ... CSCI2312 – Object Oriented Programming Section 003 Homework 3 Fall 2022 Page 7 of 7 5. Testing - Test your program. - Make sure that the changes for instance by adding a detour are reflected in the following option. o Example: ▪ Get the distance between Denver and Los Angeles ▪ Add a detour of 100miles from Denver to Los Angeles ▪ Get the distance again and compare. - Test inputs, e.g., type a city name that is not included. Your program should show an error, but your program needs to keep running. 6. Deliverables / Submission 1. Code should follow the discussed guidelines regarding naming conventions, coding style and comments. a. Comment your code. No need to comment every single line of code but add comments explaining what you are doing. 2. Develop your code in CLion. You will submit the entire
  • 16. CLion Project. 3. Create a makefile to compile and run your code in CSE. Capture a screenshot of compilation output and program running (see 4.c below). 4. You need to submit a total of three files to Canvas Assignment page: a. hwk3.zip i. A compressed file that includes all CLion files for the homework. ii. Should contain at least main.cpp, distances.h, distances.cpp, cities.txt and distances.txt. iii. Include the Readme.md file in the CLion Project. iv. Note in the Readme if you are completing or not the Extra Credit. b. makefile i. The makefile you used to compile the program in CSE Grid. c. hwk3.png or hwk3.jpg i. a screenshot of your program running on CSE Grid. ii. Include the result of option (3) between Denver and Boston. 5. Submit all your files in one single submission, otherwise you will be overwriting your previous submission. 1. The objective of this homework:2. Problem Description3. Program3.1. Implementation Details3.1.1. Provided Database Files3.1.2. Source Files3.1.3. Main Program3.1.4. Function Specification