Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with a C Program called Flight Routes 1.The initial flight will be read from a text file. The file will contain the Flight Number

Help with a C Program called Flight Routes

1.The initial flight will be read from a text file. The file will contain the Flight Number (int), Departure City Code (int), Arrival City Code (int), and Cost in dollars (int, with no cents) for each flight. There will be one flight per line. Note that there may be more than one flight with the same Departure and Arrival City code. However, the Flight Numbers are unique. Also, the first line will have a single integer value that identifies the number of flight records contained within the file. Example of Data file is attached.

2. A structure (Flight Struct) should be used with the following members:

int flightNumber;

int depCityCode;

int arrCityCode;

int cost;

3. An array of pointers to Flight structs (A Flight pointer array) will be used to contain the table of flights. NOTE: This array is not an array of Flight structs but an array of pointers to Flight structs. It should allow the user to add, remove, and search for given flights based on the Departure City and Arrival City. The Flight pointer array is a dynamically allocated table of pointers to Flight structs. Each time a flight (flight struct) is added to the table, the flight struct variable must be created using malloc() and initialized with the given flight information.

Note that if no Flight struct is assigned to a given array location, that location must be set to NULL. Also if a flight struct is removed from an array location, that location must be set to NULL *after* using free() to remove the given flight from memory.

The Syntax for the Flight Pointer array is:

Flight **FlightPointerArray;

The Syntax for accessing the struct variables is:

FlightPointerArray[i] ->depCityCode;

The Following Functions Also have to be implemented:

The initFlightPointerArray() will accept two parameters, the first being the maximum length of the Flight pointer array and the other being the file to use for initializing the Flight pointer array. Note that the inputFile may contain fewer routes than the size of the Flight pointer array. There needs to be code within this function to open, read, and close the inputFile. Also, the code must dynamically allocate each Flight struct. The Flight pointer array must also be dynamically allocated too. You will want to test this by setting the maxLength value higher than the number of flight records within the inputFile to allow for additions and removals of flight records. The function should return the number of flights created. If an error occurs (file not found, file read error, etc) it should return -1.

sample code for creating the flight pointer array with maxLength set

FlightPointer Array = malloc( maxLength * sizeof(Flight *) );

Sample code for creating a new flight entry:

FlightPointerArray [location] = malloc(sizeof(Flight));

FlightPointerArray[location]= ->depCityCode = depCode;

The findFlight() function should search sequentially through the list for a matching route with the given Departure and Arrival City codes. It should stop at the first match it finds and return the array index. If no matches are found, it should return -1. An entry is considered a match if both the given Departure City and Arrival City Codes match the ones in within the Flight struct.

int findFlight(int deptCode, int arrCode);

The printMatchingFlights() function should print out all the data for each of the Flight structs within the Flight pointer array that match the given Departure and Arrival City codes. Be sure to watch for empty (NULL) entries within the table. It must return the total number of matching flights. An entry is considered a match if both the given Departure City and Arrival City Codes match the ones in within the flight pointer array.

int printMatchingFlights (int depCode, int arrCode);

The addFlight() function will create a new Flight with the given parameters and add it to the Flight pointer array if there is an empty slot AND if there is no matching Flight Number (the Flight Numbers must be unique). It should scan the Flight pointer array sequentially looking for an empty slot (NULL Indicates empty slot). It should create a new Flight with the given parameters and assign it to the first empty slot provided there is not an already existing Flight with the same Flight number. It must use malloc() function for creating the Flight struct and store its pointer into the first available slot (array index location). It should return the array index that was used if successful, other return a -1 if there are no empty array locations left or if the given Flight number already exists with the same Flight number.

int addFlight( int flightNum, int depCode, int arrCode, int cost);

The removeFlight() function will remove the flight struct from the Fight pointer array whose Flight Number matches the given Flight Number. It will scan the Flight pointer array sequentially looking for a match. If one is found, the flight struct should be removed by calling the free() function on the matching flight and setting that array location to NULL. The removeFlight() function should return the array index of the Flight that was removed. If the given Flight number does not exist, the function should return -1.

int removeFlight(int flightNum);

Requirements:

1. Use all the functions listed above

2. The array of Flight structs must be implemented as an array of pointers to Flight Structs

3. use proper methods of malloc() and free ()

image text in transcribed

6060 5245 4155 3633 1221 ig 335 69 3456 5567 44456

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

More Books

Students also viewed these Databases questions

Question

2. Identify the purpose of your speech

Answered: 1 week ago