Question: C PROGRAMMING IN CODEBLOCKS Read the struct from the acars.bin file into an array. (You may use a dynamically-allocated array or a static one) Sort
C PROGRAMMING IN CODEBLOCKS
Read the struct from the acars.bin file into an array. (You may use a dynamically-allocated array or a static one)
Sort the array (using qsort ( )) so that it comes out in order of destination airport code, with the date and time formatted into a human-readable string.
Output the sorted array as a text file
Print out each distinct destination airport code, along with a count of the number of times it occurs in the file.
Use the C standard library function qsort ( ) to sort your array of structs. You will find that the sorting is key to simplifying the task of counting destination airport codes.
The key to using qsort ( ) is a compare ( ) function you code that makes a decision of which of a pair of elements is greater everytime it is called. This function gets called over and over again from qsort ( ) until the array ends up in ascening order by the struct element you specify.
The C standard library provides functions for converting a POSIX time into a time struct, and subsequently into a human readable string as well. Use that to get human-readable formattting for the date-time stamp read from the binary file. Now use the C standard library qsort ( ) function to sort the array on either origin or destination airport code.
With the array sorted, write a function that takes the array as a parameter, and performs the following algorithm to count how many instances of each code exist.
Set count to 0
Get a code from the array. For now, call it currCode. While you've not come to the end of the array begin
Get the next code from the array. Call it nextCode If the nextCode == currCode, then add 1 to count else output code and count, set currCode to nextCode, set count to 0 end
I HAVE MY CODE FROM THE PREVIOUS ASSIGNMENT:
#include
#include
typedef struct flightRec_struct {
char FlightNum[7];
char OriginAirportCode[5];
char DestAirportCode[5];
int timeStamp;
} myFlightRec;
// Add in your compare function here
int main(void)
{
FILE* inFile = NULL;
printf("Opening file acars.bin. ");
inFile = fopen("acars.bin", "rb");
if (inFile == NULL) {
printf("Could not open file acars.bin. ");
return -1;
}
else {
printf("File opened successfully. ");
}
printf("Processing... ");
int i;
myFlightRec flightRecVar[5000];
for (i = 0; !feof(inFile); i++) {
fread(&flightRecVar[i], sizeof(myFlightRec), 1, inFile);
printf("%s, %s, %s, %i ", flightRecVar[i].FlightNum, flightRecVar[i].OriginAirportCode, flightRecVar[i].DestAirportCode, flightRecVar[i].timeStamp);
}
//qsort here
printf("The number of flights are %d,", i++);
fclose(inFile);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
