In: Computer Science
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 will do the following:
Write the main() that will do the following:
airline: BA flights: 7 passengers: 268474
Save your files as main.c, and flights.h and upload them below for grading.
In C please
// File name: flight.h
#ifndef FLIGHT_H
#define FLIGHT_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM_FLIGHTS 200
// Defines structure Flight
struct Flight
{
// Data member to store data
char origin[4];
char destination[4];
char airline[3];
int passengers;
};// End of structure
#endif
------------------------------------------------------------------------------------------------------------------------
// File Name: main.c
#include "flights.h"
/** Function to read file contents and stores it in array of
object
* @param fileName name of the input data file
* @param flights[] an array of object to store flight records read
from the file
*
* @return the number of flights records read
*/
int readFlight(char fileName[], struct Flight flights[])
{
// To store a record read from file
char record[100];
// Loops variable
int c, d;
// Counter for number of records
int counter = 0;
// Opens the file pointer to open the file flight.txt in read
mode
FILE *rFile = fopen(fileName, "r");
// Opens the file in write binary mode and
// checks if file is unable to open for writing then display error
message
// Stop the program
if (rFile == NULL)
{
printf("Error! opening file %s for reading.", fileName);
exit(1);
}// End of if condition
// Loops till end of the file
while (!feof(rFile))
{
// Extracts a record
fgets(record, 50, rFile);
// Loops 3 times to store origin of flight
for(c = 0; c < 3; c++)
flights[counter].origin[c] = record[c];
// Stores null character at the end
flights[counter].origin[c] = '\0';
// Loops 7 times to store destination
// d is for destination index, c is for record index
for(d = 0, c = 4; c < 7; c++, d++)
// Extracts c index position character from file and stores it at d
index position of destination
flights[counter].destination[d] = record[c];
// Stores null character at the end
flights[counter].destination[d] = '\0';
// Loops 7 times to store destination
// d is for destination index, c is for record index
for(d = 0, c = 8; c < 10; c++, d++)
// Extracts c index position character from file and stores it at d
index position of destination
flights[counter].airline[d] = record[c];
// Stores null character at the end
flights[counter].airline[d] = '\0';
// Extracts the last part i.e., passenger number from
record
strncpy(record, record + ++c, strlen(record));
// Converts the passenger number to integer and stores it at
counter index position
flights[counter].passengers = (atoi(record));
// Increase the record counter by one
counter += 1;
}// End of while loop
// Close the file
fclose(rFile);
// Returns number of records
return counter;
}// End of function
/** Function to display flight information
* @param flights array of object of structure type Flight
* @param len number of records
*/
void showFlightInformation(struct Flight flights[], int len)
{
int c;
printf("\n\n ********************** Flight Information
********************** \n");
// Loops till number of flights record
for(c = 0; c < len; c++)
// Displays each record
printf("\n Origin: %s \n Destination: %s \n Airline: %s \n
Passengers: %d\n", flights[c].origin,
flights[c].destination, flights[c].airline,
flights[c].passengers);
}// End of function
/** Function to display report
* @param flights array of object of structure type Flight
* @param len number of records
*/
void showReport(struct Flight flights[], int len)
{
// To store the airline name entered by the user to search
char airlineName[5];
// To store total flight and total passenger
int totalFlight = 0, totalPassenger = 0;
int c;
// Accepts airline name
printf("\n Enter the airline name: ");
scanf("%s", airlineName);
// Loops till number of flights record
for(c = 0; c < len; c++)
{
// Checks if user entered airlineName is equals to current
index
// position airline name
if(strcmp(airlineName, flights[c].airline) == 0)
{
// Increase the flight counter by one
totalFlight++;
// Adds the current index position passengers to
totalPassengers
totalPassenger += flights[c].passengers;
}// End of if condition
}// End of for loop
printf("\n\n ********************** Flight Report for %s
********************** ", airlineName);
// Displays the report
printf("\n Airline: %s", airlineName);
printf("\n Flights: %d", totalFlight);
printf("\n Passengers: %d", totalPassenger);
}// End of function
// Main function definition
int main(int argc, char *data[])
{
// Creates an array of object of type Flight of size
NUM_FLIGHTS
struct Flight flights[NUM_FLIGHTS];
// To store number of records
int len;
// Checks if number of command line argument is less than
2
// then display error message and stop the program
if(argc < 2)
{
printf("\n ERROR NO ARGS and end the program.");
exit(0);
}// End of if condition
// Otherwise file name given
else
{
// Calls the function to read the file by passing file name and
array of object
// Stores the returned number of records
len = readFlight(data[1], flights);
// Calls the function to display all the records
showFlightInformation(flights, len);
// Calls the function to display records based on user choice of
airline
showReport(flights, len);
}// End of else
return 0;
}// End of main function
Sample Output 1: (No file name provided at command line argument)
ERROR NO ARGS and end the program.
Sample Output 2: (File name provided at command line argument)
********************** Flight Information **********************
Origin: CCA
Destination: DDA
Airline: BA
Passengers: 110
Origin: NAT
Destination: TAT
Airline: BA
Passengers: 100
Origin: CCA
Destination: DDA
Airline: MA
Passengers: 90
Origin: NAT
Destination: CCA
Airline: SA
Passengers: 70
Origin: CCA
Destination: RCA
Airline: MA
Passengers: 70
Origin: NAT
Destination: RCA
Airline: SA
Passengers: 150
Origin: DDA
Destination: NAT
Airline: BA
Passengers: 55
Enter the airline name: BA
********************** Flight Report for BA
**********************
Airline: BA
Flights: 3
Passengers: 265
flight.txt file contents
CCA,DDA,BA,110
NAT,TAT,BA,100
CCA,DDA,MA,90
NAT,CCA,SA,70
CCA,RCA,MA,70
NAT,RCA,SA,150
DDA,NAT,BA,55