Question

In: Computer Science

Data Set The data set (attached) is a modified CSV file on all International flight departing...

Data Set

The data set (attached) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation (https://data.transportation.gov/Aviation/International_Report_Passengers/xgub-n9bw). Each record holds a route (origin to destination) operated by an airline. This CSV file was modified to keep it simple and relatively smaller. Here is a description of each column:

  • Column 1 – Month (1 – January, 2 – February, 3 – March, 4 – April, 5 – May, 6 – June)
  • Column 2 – 3-letter IATA Airport Code for the origin airport (e.g., SAT for San Antonio International Airport)
  • Column 3 – 3-letter IATA Airport Code for the destination airport
  • Column 4 – 2-letter IATA Airline Code for the airline (e.g., AA for American Airlines). Some airlines will have a 3-letter airline code, your program will exclude them from the parsing.
  • Column 5 – The passenger category, in our example, there is only one category.
  • Column 6 – Total number of passengers in that month for that route

Note that there is a header row you must skip. Since this data holds passenger statistics for each route operated by an airline for six months, you should see the airline route repeated six times. For example, you will see the JFK to LHR operated by BA route 6 times, once for each of the six months.

Task 1 – create route-records.h

All data is loaded into an array of RouteRecord’s which will they be queried in main().

  • Create a struct named RouteRecord that will hold information about a route that is operated by one airline. The struct will have the following data members:
    • Origin airport code
    • Destination airport code
    • Airline code
    • Array of passenger counts. There are six months’ worth of data for each route. (Index 0 will represent January’s passenger count, Index 1 will represent February’s passenger count, etc.).
  • Add the header guards and prototypes for the functions (see Task 2)
  • Include this enum in your header file so you can use as values for determining what type of search you will conduct.
    typedef enum SearchType { ROUTE, ORIGIN, DESTINATION, AIRLINE } SearchType;

Task 2 – create route-records.c

Write the following functions:

  • RouteRecord* createRecords( FILE* fileIn ) – This function creates the array of RouteRecord’s and initializes it. The function takes in a file pointer. The function will do the following:
    • This function goes through the CSV file and counts the number of total records (not including the header)
    • Dynamically allocate memory for an array of RouteRecord’s based on the count.
    • Each RouteRecord struct object has an array of 6 integers to hold the number of passengers for six months. Initialize each of these integer values to 0. You do not need to initialize the other data members in the struct.
    • Rewind the file pointer
    • Return the pointer to the array you dynamically allocated.
  • int fillRecords( RouteRecord* r, FILE* fileIn ) – This function will process the data in the CSV file. Essentially, the code will go through each record, parse out the record, and enter it into the array. The function will follow these rules:
    • If the record contains an airline that has 3 letters, ignore this record and go to the next record.
    • The function will call findAirlineRoute() to see if the exact route with the origin, destination, and airline was already entered in the array. If it was found, then you will update the existing record in your array with the passenger data for that month. Recall there should be six entries (one for each month) for each route operated by an airline. If the route operated by the airline does not already exist in the array, add this new route to the array.
    • The function returns the actual number of RouteRecord’s used in the array. The value returned will be less than the size of the array created since not all records in the original CSV file will be entered into the array.
  • int findAirlineRoute( RouteRecord* r, int length, const char* origin, const char* destination, const char* airline, int curIdx ) – This RECURSIVE function finds a record in the RouteRecord array with the same origin and destination airport codes and airline. It returns the index number in which these three strings appear in the array. The function will return -1 if it cannot find these three strings in the same struct object.
  • void searchRecords( RouteRecord* r, int length, const char* key1, const char* key2, SearchType st ) – This function searches the RouteRecord array and prints out the results of the search.
    • You will traverse the array and compare specific data members against the keys.
    • The parameter st determines if the function is searching by ROUTE, ORIGIN, DESTINATION, AIRLINE.
    • For ORIGIN, DESTINATION, AIRLINE, key1 will hold the value you are looking for. For ROUTE, you are searching both the origin and destination and airport, so key1 and key2 will hold those values, respectively, that you will use to compare against the data members. For example, if the search is by the destination: st will be equal to DESTINATION, key1 will have an airport code that the user entered, and you will compare each struct’s destination data member against the airport code.
    • You will print out the airline and the route for each matching value. Then, you will print out the total number of passengers on all matching records, total number of passengers by month for all matching records, as well as average numbers of passengers per month. Note that you must handle any instances where you the search has 0 results.
  • void printRecords( RouteRecord* r, int length) – This function prints the records. rdenotes the pointer for the records and length is the number of records to be printed. You can also create another helper function that just prints one record - void printRecord( RouteRecord r ). The printRecords function can call the printRecord function to print one record at a time.
  • void printMenu() – This function prints the menu. Here is the function below. Be sure to add this prototype to the header file.

void printMenu()

{

printf( "\n\n######### Airline Route Records Database MENU #########\n" );

printf( "1. Search by Route\n" );

printf( "2. Search by Origin Airport\n" );

printf( "3. Search by Destination Airport\n" );

printf( "4. Search by Airline\n" );

printf( "5. Quit\n" );

printf( "Enter your selection: " );

}

TASK 3: Complete the project2-main.c

  • Download the attached project2-main.c
  • Follow the instructions written in the comments in the main() function.
  • The main() is the driver of the program. It calls the functions above to load the data from the CSV file and to run queries that the user asks for.
  • The name of the file will be passed in through command line arguments.
  • The user will enter a numeric value from the menu. You must handle the case in which the user enters invalid values (e.g., strings).

Task 4: Create a makefile

Create a makefile to compile and link all the files together. The grader will compile your code using your makefile.

Solutions

Expert Solution

Working code implemented in C and appropriate comments provided for better understanding.

Here I am attaching code for all files:

  • main.c
  • routeRecords.c
  • routeRecords.h
  • makefile

main.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "routeRecords.h"

int main(int argc, char *argv[])
{
char inputName[20];
strcpy(inputName, argv[1]);

FILE *inputFile;
printf("Opening passenger-data.csv... \n");
inputFile = fopen(inputName, "r");
checkFileOpening(inputFile);

RouteRecord *flights;
flights = createRecords(inputFile);

int flightsLength;
flightsLength = fillRecords(flights, inputFile);

int i;
char key1[5];
char key2[5];
SearchType searchKind = 0;
while (searchKind != 5)
{
searchKind = getMenuOption(key1, key2);
stringUpper(key1);
stringUpper(key2);
searchRecords(flights, flightsLength, key1, key2, searchKind);
}

return 0;
}

routeRecords.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//

// SearchType is the enumeration of the options for a search.
//Searches can be a ROUTE (origin to destination),ORIGIN only, DESTINATION only, AIRLINE company, or a QUIT command.
typedef enum SearchType
{
ROUTE = 1,
ORIGIN,
DESTINATION,
AIRLINE,
QUIT,
} SearchType;

// RouteRecord is the combination of a flight's origin, destination, id, and number of passengers
typedef struct RouteRecord_struct
{
char origin[4];
char destination[4];
char airline[4];
int passengers[7];
} RouteRecord;

// checkFileOpening returns -1 if inputFile opened improperly, and 0 otherwise
int checkFileOpening(FILE *inputFile)
{
int output = 0;

if (inputFile == NULL)
{
printf("Error\n");
output = -1;
}
return output;
}

// getFileLength returns an int of the length of fileName
int getFileLength(FILE *inputFile)
{
//creates file pointer
int output;
//counts the number of lines
char buffer[1000];
while (!feof(inputFile) && output != -1)
{
fgets(buffer, 1000, inputFile);
output++;
}
rewind(inputFile);
return output;
}

// createRecords returns a pointer to the start of a RouteRecords array from the File pointer, it accounts for headers
RouteRecord *createRecords(FILE *inputFile)
{
RouteRecord *flights;
int fileLength = getFileLength(inputFile);
fileLength -= checkCSVHeader(inputFile);
flights = (RouteRecord *)malloc(sizeof(RouteRecord) * fileLength);
return flights;
}
// stringUpper takes a string and uppercases all of it
void stringUpper(char *lowered)
{
int i;
for (i = 0; i < strlen(lowered); i++)
{
lowered[i] = toupper(lowered[i]);
}
}

// checkCSVHeader returns 1 if there is a file header, and 0 if there is none, from a parameter of an input file
// a CSV file header must start with "Month"
int checkCSVHeader(FILE *inputFile)
{

char buffer[1000];
fgets(buffer, 1000, inputFile);
char tester[5];
sscanf(buffer, "%5[^,]", tester);
stringUpper(tester);
if (strcmp(tester, "MONTH") == 0)
{
rewind(inputFile);
return 1;
}
rewind(inputFile);
return 0;
}

// fillRecords returns the usable length of an array flights, and fills flights with data from an inputFile
// fillRecords assumes file is sorted by months
int fillRecords(RouteRecord *flights, FILE *inputFile)
{
int month;
char origin[4];
char destination[4];
char airline[4];
char flightType[20];
int passengers;

char buffer[1000];
int flightsLength = 0;
int flightIndex;
int passengersIndex;

int fileLength = getFileLength(inputFile);
int i = checkCSVHeader(inputFile);
if (i == 1)
{
fgets(buffer, 1000, inputFile);
}

while (i < fileLength && !feof(inputFile))
{
fgets(buffer, 1000, inputFile);
sscanf(buffer, "%d,%3[^,],%3[^,],%3[^,],%20[^,],%d", &month, origin, destination, airline, flightType, &passengers);
//printf("%d|%s|%s|%s|%s|%d\n", month, origin, destination, airline, flightType, passengers);
if (strlen(airline) != 3)
{
flightIndex = findAirlineRoute(flights, flightsLength, origin, destination, airline);
if (flightIndex == -1)
{
strcpy((*(flights + flightsLength)).origin, origin);
strcpy((*(flights + flightsLength)).destination, destination);
strcpy((*(flights + flightsLength)).airline, airline);
for (passengersIndex = 0; passengersIndex < 6; passengersIndex++)
{
(*(flights + flightsLength)).passengers[passengersIndex] = 0;
}
(*(flights + flightsLength)).passengers[month - 1] = passengers;
flightsLength++;
}
else
{
(*(flights + flightIndex)).passengers[month - 1] = passengers;
}
}
i++;
}

printf("Unique routes operated by airlines: %d\n", flightsLength);
return flightsLength;
}

// findAirlineRoute returns -1 if the inputted RouteRecord data is new to an inputted array, in the case of a duplicate it returns the index in flights the dupe is.
int findAirlineRoute(RouteRecord *flights, int flightsLength, char *origin, char *destination, char *airline)
{
int test = -1;
int i = 0;
while (i < flightsLength && test == -1)
{
RouteRecord compare = (*(flights + i));
if (strcmp(compare.origin, origin) == 0)
{
if (strcmp(compare.destination, destination) == 0)
{
if (strcmp(compare.airline, airline) == 0)
{

test = i;
}
}
}
i++;
}
return test;
}

// printMenu prints the script to direct the user for input
void printMenu()
{
printf("\n\n######### Airline Route Records Database MENU #########\n");
printf("1. Search by Route\n");
printf("2. Search by Origin Airport\n");
printf("3. Search by Destination Airport\n");
printf("4. Search by Airline\n");
printf("5. Quit\n");
printf("Enter your selection: ");
}

// getMenuOption returns the enum of choice of the user, and fills the search keys key1 and key2
SearchType getMenuOption(char *key1, char *key2)
{
printMenu();
int searchKind;
int checker;
char buf[100];

do
{
checker = scanf("%d", &searchKind);
while (checker == 0)
{
printf("Error: invalid option\n");
printf("Please enter an INTEGER option:");
fgets(buf, 100, stdin);
checker = scanf("%d", &searchKind);
}

switch (searchKind)
{
case ROUTE:
printf("Enter the origin: ");
scanf("%s", key1);
printf("Enter the destination: ");
scanf("%s", key2);
break;

case ORIGIN:
printf("Enter the origin: ");
scanf("%s", key1);
break;

case DESTINATION:
printf("Enter the destination: ");
scanf("%s", key1);
break;

case AIRLINE:
printf("Enter the airline: ");
scanf("%s", key1);
break;

case QUIT:
break;

default:
printf("Error: invalid option\n");
printf("Please choose an option 1-5: ");
break;
}
} while (searchKind != ROUTE && searchKind != ORIGIN && searchKind != DESTINATION && searchKind != AIRLINE && searchKind != QUIT);
return searchKind;
}

// searchRecords prints search results from an list of flights, its length, search keys, and search type
void searchRecords(RouteRecord *flights, int flightsLength, char *key1, char *key2, SearchType searchKind)
{
int i;
int j;
int matches = 0;
RouteRecord *compare;

int totalPassengers[6] = {0, 0, 0, 0, 0, 0};
printf("\n");
switch (searchKind)
{
case ROUTE:
printf("Searching by route...\n");
for (i = 0; i < flightsLength; i++)
{
if (strcmp(flights[i].origin, key1) == 0 && strcmp(flights[i].destination, key2) == 0)
{
matches++;
printf("%s (%s-%s) ", flights[i].airline, flights[i].origin, flights[i].destination);
for (j = 0; j < 6; j++)
{
(totalPassengers[j]) += flights[i].passengers[j];
}
if (i == 10)
{
printf("\n");
}
}
}
break;

case ORIGIN:
printf("Searching by origin...\n");
for (i = 0; i < flightsLength; i++)
{
if (strcmp(flights[i].origin, key1) == 0)
{
matches++;
printf("%s (%s-%s) ", flights[i].airline, flights[i].origin, flights[i].destination);
for (j = 0; j < 6; j++)
{
(totalPassengers[j]) += flights[i].passengers[j];
}
if (i == 10)
{
printf("\n");
}
}
}
break;

case DESTINATION:
printf("Searching by destination...\n");
for (i = 0; i < flightsLength; i++)
{
if (strcmp(flights[i].destination, key1) == 0)
{
matches++;
printf("%s (%s-%s)", flights[i].airline, flights[i].origin, flights[i].destination);
for (j = 0; j < 6; j++)
{
totalPassengers[j] += flights[i].passengers[j];
}
if (i == 10)
{
printf("\n");
}
}
}

case AIRLINE:

printf("Searching by airline...\n");
for (i = 0; i < flightsLength; i++)
{
if (strcmp(flights[i].airline, key1) == 0)
{
printf("%s (%s-%s)", flights[i].airline, flights[i].origin, flights[i].destination);
for (j = 0; j < 6; j++)
{
totalPassengers[j] += flights[i].passengers[j];
}
if (i == 10)
{
printf("\n");
}
}
}

break;

default:
break;
}
printf("\n%d matches were found.\n", matches);
printf("\n");
printf("Statistics:\n");
int totaltotal = totalPassengers[0] + totalPassengers[1] + totalPassengers[2] + totalPassengers[3] + totalPassengers[4] + totalPassengers[5];
printf("Total Passengers: %d\n", totaltotal);
for (i = 0; i < 6; i++)
{
printf("Total Passengers in Month %d: %d\n", i + 1,
totalPassengers[i]);
}
printf("\n");
printf("Average Passengers per month: %d\n", totaltotal / 6);
printf("\n");
}

routeRecords.h:

#ifndef ROUTERECORDS_H
#define ROUTERECORDS_H

typedef enum SearchType
{
ROUTE,
ORIGIN,
DESTINATION,
AIRLINE,
QUIT
} SearchType;

typedef struct RouteRecord_struct
{
char origin[3];
char destination[3];
char airline[3];
int passengers[6];
} RouteRecord;

void printMenu();
int checkFileOpening(FILE *inputFile);
int getFileLength(FILE *inputFile);
int checkCSVHeader(char *inputFile);
RouteRecord *createRecords(FILE *inputFile);
void searchRecords(RouteRecord *flights, int flightsLength, char *key1, char *key2, SearchType st);
int fillRecords(RouteRecord *flights, FILE *inputFile);
int findAirlineRoute(RouteRecord *flights, int flightsLength, char *origin, char *destination, char *airline);
SearchType getMenuOption();
int createRecordsTest(FILE *inputFile);
RouteRecord* fillRecordsTest(RouteRecord *flights, FILE *inputFile);
void stringUpper(char *lowered);

#endif

makefile:

routesearch: main.o routeRecords.o
   gcc main.o routeRecords.o -o routesearch
main.o: main.c routeRecords.h
   gcc -c main.c
routeRecords.o: routeRecords.c routeRecords.h
   gcc -c routeRecords.c
clean:
   rm *.o routesearch

Sample Output Screenshots:


Related Solutions

Destination Departing Flight Numbers (list all departing flight segments) Distance (round to nearest mile) Amount Miami...
Destination Departing Flight Numbers (list all departing flight segments) Distance (round to nearest mile) Amount Miami Delta 3899/ Delta 951 993 $328 San Diego Delta 3899/ Delta 1909 2,321 $609 New York City Delta 3899/ Delta 2021 555 $508 Chicago Delta 3899/ Delta 1608 516 $205 Seattle Delta 3899/Delta 3642 2,568 $491 Salt Lake City Delta 2899/ Delta 2611 1,831 $475 Boston Delta 2109/ Delta 665 744 $579 Honolulu Delta 876/ Delta 1559 4,594 $1,168 Denver Delta 3899/ Delta 2871...
7. Chapter 13, Question 2: Use the attached data file “Chapter 13 Data Set 1” to...
7. Chapter 13, Question 2: Use the attached data file “Chapter 13 Data Set 1” to answer this question in the book. Do you agree with the author’s conclusion about whether practice time makes a difference? <15 Hours Practice 15-25 Hours Practice More than 25 Hours Practice 58.7 64.4 68 55.3 55.8 65.9 61.8 58.7 54.7 49.5 54.7 53.6 64.5 52.7 58.7 61 67.8 58.7 65.7 61.6 65.7 51.4 58.7 66.5 53.6 54.6 56.7 59 51.5 55.4 54.7 51.5 61.4...
Using the data from the csv file, answer the questions with rstudio # number_children - The...
Using the data from the csv file, answer the questions with rstudio # number_children - The number of children in the home # internet - Does the home have internet access? # mode - The way the household took the survey # own - Do the residents own with or without a mortgage or rent? # language - The primary language spoken in the home # decade_built - The decade the home was built 1) In how many households, wife’s...
Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
When using the import wizard in MATLAB to import data fro, a .csv file the data...
When using the import wizard in MATLAB to import data fro, a .csv file the data appears in MATLAB in the following format "35:53.2" how do I convert this into more usable matlab values? I think that the duration function was used to generate the time format. The code will need to be written in MATLAB software I will leave feedback if you are able to provide a correct response. Thank you
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
The data set for this question set (Tab Q1 in the Excel data file) comes from...
The data set for this question set (Tab Q1 in the Excel data file) comes from a research project that tracks the elderly residents in a community to monitor their cognitive function and general health. Based on the literature, education is considered a protective factor against dementia, and memory decline is usually the first sign of dementia. So the researchers would like to know whether education level (measured in number of years of formal schooling) is correlated with memory function...
2. The data set `MLB-TeamBatting-S16.csv` contains MLB Team Batting Data for selected variables. Load the data...
2. The data set `MLB-TeamBatting-S16.csv` contains MLB Team Batting Data for selected variables. Load the data set from the given url using the code below. This data set was obtained from [Baseball Reference](https://www.baseball-reference.com/leagues/MLB/2016-standard-batting.shtml). * Tm - Team    * Lg - League: American League (AL), National League (NL) * BatAge - Batters’ average age * RPG - Runs Scored Per Game * G - Games Played or Pitched * AB - At Bats * R - Runs Scored/Allowed * H...
The data in the attached excel file comes from Consumer Reports and was collected over a...
The data in the attached excel file comes from Consumer Reports and was collected over a two-year period. It gives the average mpg over a 195-mile trip, the weight of the vehicle (pounds), engine displacement (liters), number of cylinders, horsepower, type of transmission (0 = manual, 1 = automatic), the number of gears and whether the car was foreign (1) or domestic (0). Part a: Build a model for predicting the average mpg based on the data in the attached...
You have an Excel file attached to this link. You are to first transpose the data...
You have an Excel file attached to this link. You are to first transpose the data so that, instead of being in the horizontal format, it will be converted to the vertical form. Then to run a Multiple Regression and see which of the independent variables are significant and whether the overall model is significant. You also should be able to comment on the goodness of the fit. All of these require that you have thoroughly watched the video lectures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT