[ad_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 15×15 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 —————————— 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 , 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 “, 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 “, 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 “, XXX miles away and , 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 “, XXX miles away and , 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: – – 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.

[ad_2]

Testimonials

C++
We have updated our contact contact information. Text Us Or WhatsApp Us+1-(309) 295-6991