SlideShare a Scribd company logo
1 of 17
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

Similar to CSCI2312 – Object Oriented Programming Section 003 Homewo

Machine Learning Approach to Report Prioritization with an ...
Machine Learning Approach to Report Prioritization with an ...Machine Learning Approach to Report Prioritization with an ...
Machine Learning Approach to Report Prioritization with an ...
butest
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
Aggarwalelectronic18
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docx
rosemarybdodson23141
 
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop ClustersHDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
Xiao Qin
 
2004 map reduce simplied data processing on large clusters (mapreduce)
2004 map reduce simplied data processing on large clusters (mapreduce)2004 map reduce simplied data processing on large clusters (mapreduce)
2004 map reduce simplied data processing on large clusters (mapreduce)
anh tuan
 

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

EFFICIENT CALL PATH DETECTION FOR ANDROID-OS SIZE OF HUGE SOURCE CODE
EFFICIENT CALL PATH DETECTION FOR ANDROID-OS SIZE OF HUGE SOURCE CODEEFFICIENT 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 ...
Machine Learning Approach to Report Prioritization with an ...Machine Learning Approach to Report Prioritization with an ...
Machine Learning Approach to Report Prioritization with an ...
 
ThesisPresentation
ThesisPresentationThesisPresentation
ThesisPresentation
 
Reporting Summary Information of Spatial Datasets and Non-Compliance Issues U...
Reporting Summary Information of Spatial Datasets and Non-Compliance Issues U...Reporting Summary Information of Spatial Datasets and Non-Compliance Issues U...
Reporting Summary Information of Spatial Datasets and Non-Compliance Issues U...
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
 
R Spatial Analysis using SP
R Spatial Analysis using SPR Spatial Analysis using SP
R Spatial Analysis using SP
 
Computation Assignment Help
Computation Assignment Help Computation Assignment Help
Computation Assignment Help
 
IRJET- Survey on Implementation of Graph Theory in Routing Protocols of Wired...
IRJET- Survey on Implementation of Graph Theory in Routing Protocols of Wired...IRJET- Survey on Implementation of Graph Theory in Routing Protocols of Wired...
IRJET- Survey on Implementation of Graph Theory in Routing Protocols of Wired...
 
Unit 2 part-2
Unit 2 part-2Unit 2 part-2
Unit 2 part-2
 
Monzor, Carbon-R-a, and the end of the world
Monzor, Carbon-R-a, and the end of the worldMonzor, Carbon-R-a, and the end of the world
Monzor, Carbon-R-a, and the end of the world
 
Name _______________________________ Class time __________.docx
Name _______________________________    Class time __________.docxName _______________________________    Class time __________.docx
Name _______________________________ Class time __________.docx
 
Description Of A Graph
Description Of A GraphDescription Of A Graph
Description Of A Graph
 
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop ClustersHDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
HDFS-HC: A Data Placement Module for Heterogeneous Hadoop Clusters
 
Project Report
Project ReportProject Report
Project Report
 
Comparative Analysis of Distance Vector Routing & Link State Protocols
Comparative Analysis of Distance Vector Routing & Link State ProtocolsComparative Analysis of Distance Vector Routing & Link State Protocols
Comparative Analysis of Distance Vector Routing & Link State Protocols
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
Map reduce
Map reduceMap reduce
Map reduce
 
2004 map reduce simplied data processing on large clusters (mapreduce)
2004 map reduce simplied data processing on large clusters (mapreduce)2004 map reduce simplied data processing on large clusters (mapreduce)
2004 map reduce simplied data processing on large clusters (mapreduce)
 
Lecture 1 mapreduce
Lecture 1  mapreduceLecture 1  mapreduce
Lecture 1 mapreduce
 
Project on nypd accident analysis using hadoop environment
Project on nypd accident analysis using hadoop environmentProject on nypd accident analysis using hadoop environment
Project on nypd accident analysis using hadoop environment
 

More from simisterchristen

Reflecting on Personal Identity and Global CitizenshipReview the .docx
Reflecting on Personal Identity and Global CitizenshipReview the .docxReflecting on Personal Identity and Global CitizenshipReview the .docx
Reflecting on Personal Identity and Global CitizenshipReview the .docx
simisterchristen
 
Reflecting on Personal Identity and Global CitizenshipReview the.docx
Reflecting on Personal Identity and Global CitizenshipReview the.docxReflecting on Personal Identity and Global CitizenshipReview the.docx
Reflecting on Personal Identity and Global CitizenshipReview the.docx
simisterchristen
 
ReferencesAssignment Submit a reference list showing your r.docx
ReferencesAssignment Submit a reference list showing your r.docxReferencesAssignment Submit a reference list showing your r.docx
ReferencesAssignment Submit a reference list showing your r.docx
simisterchristen
 
Recommended Pages 5Style MLACitations Have a works cited page.docx
Recommended Pages 5Style MLACitations Have a works cited page.docxRecommended Pages 5Style MLACitations Have a works cited page.docx
Recommended Pages 5Style MLACitations Have a works cited page.docx
simisterchristen
 

More from simisterchristen (20)

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

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.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