Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#pragma warning(disable: 4996) #include #include #define FALSE 0 #define TRUE 1 #define INPUT_BUF_MAX 80 #define NUM_CITIES 6 #define CITY_OFFSET 1 int checkRange(int input, int rangeMin,

#pragma warning(disable: 4996) #include #include #define FALSE 0 #define TRUE 1 #define INPUT_BUF_MAX 80 #define NUM_CITIES 6 #define CITY_OFFSET 1 int checkRange(int input, int rangeMin, int rangeMax); int getNumNicely(char* question); int getNum(void); int getRoute(char *cityNames[], int flyingTimes[], int layoverTimes[], int home, int dest); void printAirports(char *cityNames[]); void menu(char **cityNames, int *flyingTimes, int *layoverTimes); void populateCities(char *cityNames[], int flyingTimes[], int layoverTimes[]); int checkRange(int input, int rangeMin, int rangeMax) { // Define and assign the return value to it's false state int result = 0; // Check whether the input is within the predefined range if (input >= rangeMin && input <= rangeMax) { // Return result as 1 since it is within its range result = 1; } else { // Return result as 0 since it is not within its range // Since result has been already assigned 0, there is nothing to do here } return result; }

int getNumNicely(char* question) { printf("%s ", question); return getNum(); } int getNum(void) { /* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */ char record[INPUT_BUF_MAX] = { 0 }; /* record stores the string */ int number = 0; /* NOTE to student: indent and brace this function consistent with your others */ /* use fgets() to get a string from the keyboard */ fgets(record, INPUT_BUF_MAX, stdin); /* extract the number from the string; sscanf() returns a number * corresponding with the number of items it found in the string */ if (sscanf(record, "%d", &number) != 1) { /* if the user did not enter a number recognizable by * the system, set number to -1 */ number = -1; } return number; } int getRoute(char *cityNames[], int flyingTimes[], int layoverTimes[], int home, int dest) { int result = 0; int airplane = home; // Creative name for iterator int travelTime = 0; // In minutes

if (home > dest) { printf("You can't go backwards. "); result = 2; } else if (home < dest) { printf("Departing from (#%d) %s for arrival at (#%d) %s ", home + CITY_OFFSET, cityNames[home], dest + CITY_OFFSET, cityNames[dest]); printf("Home -> Destination:\tFlying Time:\tLayover Time: "); for (airplane = home; airplane < dest; airplane++) { // Check if this is the last leg of the trip if (airplane == dest - 1) { //If it is, kick off the layover time as we're not continuing on printf("%s -> %s\t%02d:%02d\t\t ", cityNames[airplane], cityNames[airplane + 1], flyingTimes[airplane] / 60, flyingTimes[airplane] % 60); //add the minutes of the flight to our total flight length travelTime += flyingTimes[airplane]; } else { printf("%s -> %s\t%02d:%02d\t\t%02d:%02d ", cityNames[airplane], cityNames[airplane + 1], flyingTimes[airplane] / 60, flyingTimes[airplane] % 60, layoverTimes[airplane] / 60, layoverTimes[airplane] % 60); //add the minutes of the flight and layover to our total flight length travelTime += flyingTimes[airplane] + layoverTimes[airplane]; } } // Display the final trip length printf("Your trip will take %02d:%02d:%02d ", (travelTime / 60) / 24, (travelTime / 60) % 24, travelTime % 60); } else { // Alert the user if their starting and ending cities are the same printf("Turn around! You're already there! "); result = 1; }

return result; } void printAirports(char *cityNames[]) { int i = 0; printf("Airports: "); // Loop through all of the cities and print out their names for (i = 0; i < NUM_CITIES; i++) { printf("(#%d) %s ", i + CITY_OFFSET, cityNames[i]); } printf(" "); } void menu(char *cityNames[], int flyingTimes[], int layoverTimes[]) { int exit = FALSE; // Exit sentinal int input = 1; // Input buffer int home = 1; int destination = 1; while (!exit) { // Let the user know what airports are available printAirports(cityNames); input = getNumNicely("Please enter the number of your starting city airport"); if (checkRange(input, CITY_OFFSET, NUM_CITIES)) { // Home city is validated // Save the home city home = input; } else if (input == 0) { // Exit sentinal has been reached printf("You entered 0. Now exiting "); exit = TRUE; continue; } else { printf("Invalid input. Please try again. "); continue; } input = getNumNicely("Please enter the number of your ending city airport"); if (checkRange(input, CITY_OFFSET, NUM_CITIES)) { // Home city is validated // Save the destination city destination = input; } else { printf("Invalid input. Please try again. "); continue; } printf(" "); // Print off the route information from the home airport to the destination airport getRoute(cityNames, flyingTimes, layoverTimes, home - CITY_OFFSET, destination - CITY_OFFSET); printf(" "); } } int main(void) { // Populate the buffers char *cityNames[NUM_CITIES] = { "Toronto", "Atlanta", "Austin", "Denver", "Chicago", "Buffalo" }; int flyingTimes[NUM_CITIES - 1] = { (4 * 60) + 15, (3 * 60) + 58, (3 * 60) + 55, (2 * 60) + 14, (3 * 60) + 27 }; int layoverTimes[NUM_CITIES - 1] = { (1 * 60) + 20, (0 * 60) + 46, (11 * 60) + 29, (0 * 60) + 53, 0 }; //populateCities(cityNames, flyingTimes, layoverTimes);

menu(cityNames, flyingTimes, layoverTimes); // free your memory free(cityNames); free(flyingTimes); free(layoverTimes);

return 0; }

help me modify the c program above by following the requirements below Functional Requirements The user must provide the starting and ending cities as a number. The numbers corresponding to each city are shown in the table above (e.g. Toronto is city 1, Kansas City is city 5). Do not change the numbers for the cities. Your program must loop until the user selects city 0 for the starting city. In the loop, display the city list (with city number and city name on the same line, one line per city) and then ask the user for the starting city and then for the ending city. Once you get the ending city, calculate and display the elapsed time to travel between the cities if they are valid OR display an error message if either of the cities are not valid. The result must be calculated. It must then be displayed in correct hh:mm format, not hours only or minutes only. The result should include all layovers between cities but not the layover once you get into the ending city. If the number of hours is less than 10, you must not have a leading 0 (i.e. "9:43" is OK, "09:43" is not). If the number of minutes is less than 10, you must have a leading 0 (i.e. "9:03" is OK, "9:3" is not). If the cities are valid, the output line must be exactly of the format "[city1name] to [city2name] will take [hh:mm]." Make sure that this is exact, even down to the punctuation and spacing. The error message is described below. This output line must also be written to a text file called "results.txt", stored in the current directory. Only these specific lines should be written to the file. I'm serious about not changing the city numbers. Do not have extra user input (including "Press any key to continue") beyond getting the starting and ending cities repeatedly. I am very serious about this. Don't clear the screen when doing output. Spelling and consistency in your output is important. Also, make good use of blank lines to separate unrelated parts of your output. The program must not produce incorrect output or crash or hang if invalid input is used. You must use either cin or the getNum() function from Assignment 2 for getting input. You are not responsible for handling excessively long input lines or numbers out of the range of an int variable. Programming Requirements It must use at least one non-trivial (i.e. more than one or two lines long) user-created function other than main() and the function you use for getting user input. Parameters must be passed and/or return values used. It must not use global variables or goto. Any use of scanf() will be handled as described in class. The city names and flying and layover times for a city must be stored in a struct called CityInfo. All of this data must be stored as an array of structs stored in main(). This array must be passed to a function that does the time calculations (so that I can see that you know how to do it). The addition of the times must be done using a loop of some type. The flying and layover times must be loaded in from a binary file. Each time is a pair of two bytes. The first byte is the hours portion. The second byte is the minutes portion. For example, the time "4:15" is stored as one byte with the ASCII value 4 and a second byte with the ASCII value 15. It is your job to figure out how to convert this to minutes (which is easiest to work with). The name of the flying and layover times file must be obtained as the first command-line argument. For your internal testing, I will provide this binary file for you. I will test using this provided file but will use whatever filename I want. Loading this binary file into a text editor likely won't work very well for you. The city names must be loaded in from a text file. Each city name is stored in the file as a string on one line (so there should be 6 lines in the file). The name of the city name file must be obtained as the second command-line argument. You must create this file yourself and submit it, although I will use my own version. The city names must be read in from the file as C-style null-terminated strings using fgets() but they can be stored in the struct as either a C-style null-terminated string or a C++ string. The "results.txt" filename must be stored as a const char * variable called resultsFilename (i.e. const char *resultsFilename = "results.txt";) that you would then use in your fopen() call and error messages. This constant can be global. Do not hardcode the "results.txt" filename in your fopen() call. It would also be a good idea to not hardcode the "results.txt" filename in your error messages but I won't be checking that when marking. Use best practices with respect to Magic Numbers. Assume that the user will always enter less than 80 characters. The program must be commented adequately and indented correctly. Errors If there is not exactly two command-line arguments, display an error message that states exactly "Usage: cA3 " and quits the program immediately.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago