Question

In: Computer Science

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 will do the following:

    • Include the header guards
    • Declare a struct named Flight. It will have the following data members: origin (a 3 letter airport code), destination (a 3 letter airport code), airline (a 2 letter airline code), and passengers (a count of the passengers on that flight). Remember that a C-style string needs to have one extra null character at the end.
    • Write a preprocessor directive to represent a constant value for an array size. Name it NUM_FLIGHTS and give the replacement value 200.
    • There will be no separate source file for this header file.
  • Write the main() that will do the following:

    • Read in the name of the file through the command line argument. If the argument is not there, print out the error message: ERROR NO ARGS and end the program.
    • Open the file for reading. If the file does not open, print out the error message: ERROR FILE NOT OPEN and end the program.
    • Dynamically allocate an array of 200 Flight struct objects.
    • Read in and parse the CSV into this array.
    • Ask the user to enter a two letter airline code. Then, traverse the array and count two things: the number of flights that airline operates and the total number of passengers for those flights.
    • Close the file and free the dynamic array when you are done.
    • The output must look exactly like below in terms of formatting. There is only one space after the colon.
airline: BA
flights: 7
passengers: 268474

Save your files as main.c, and flights.h and upload them below for grading.

In C please

Solutions

Expert Solution

// 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


Related Solutions

Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
Write a program that reads and parses the CSV file, and can sort and filter data...
Write a program that reads and parses the CSV file, and can sort and filter data according to the user input and print the results. Your program should be able to do the following according to the user input: 1. Show results from a specific country 2. Sort the data according to any column 3. Filter the results (for example, >10000 cases) 4. Print the top or bottom n results You get bonus points if your program • Can do...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
Write a program that reads in a continuous stream of strings representing a line of CSV...
Write a program that reads in a continuous stream of strings representing a line of CSV data in the format "NAME,AGE,EMAIL,DOB". Implement a function check_csv which verifies that each input string matches this format by ensuring that: • There are 4 tokens in the string corresponding to the 4 properties above. • The second token MUST be a number. • The fourth token should be in the format MM-DD-YYYY (hint: tokenize this as well). The function should return 0 if...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
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,...
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT