Question

Consider the data file “assignment-07-input.csv. The data contains of 4 different things separated by comma –...

Consider the data file “assignment-07-input.csv.

The data contains of 4 different things separated by comma – origin airport code (3 characters),

destination airport code (3 characters), airline code (2 characters), passenger count (integer).

There is no header row, so you do not have to deal with it. Write code to do the following:

• The program will take 3 extra command line arguments & use a makefile to run.

o Input file name

o Output file name

o Airline code

• Once run, the program should calculate the count the number of passengers for the

input airline code and write the count to the output file “assignment-07-output.txt”.

Your program should validate the command line arguments before continuing with the

main program execution.

• Read and parse the CSV file and store the data in a structure “RouteRecord”. Each

RouteRecord will have the following data members

o origin airport code (3 characters - SAT)

o destination airport code (3 characters - SAT)

o airline code (2 characters - AA)

o passenger count (integer)

• Create 3 separate files – routerecords.h, routerecords.c & a7.c

• Create different functions that are needed to read data from the file. Make sure to have

proper prototypes, header guards, header includes, etc.

o You must dynamically create an array of RouteRecords.

o The file will not contain the number of RouteRecords, you should read the file

once to know the value of N and then populate the array.

• Create function(s) which goes through the data array and calculates the number of

passengers for the input airline code. Write the number to the output file.

• You need to create a MAKEFILE.

• An example to run the program using the makefile would be

 make

 ./a7 assignment-07-input.csv assignment-07-output.csv AA

***csv file**

Part of Data Set

JFK LHR BA 87133
JFK LHR VS 61962
JFK CDG AF 61178
LGA YYZ AC 60792
SFO TPE BR 57801
MIA LHR BA 56095
JFK STI B6 55885
SFO HKG CX 55012
JFK DXB EK 54746
LAX TPE BR 54741
FLL YUL RV 54217
LAX ICN OZ 54103
LAX HKG CX 52285
DFW CUN AA 51561
JFK SDQ B6 51402
DFW LHR AA 49292
ATL CUN DL 49223
MIA PTY CM 48212
GUM NRT UA 46674
LAX PEK CA 46434
MIA CUN AA 45570
LAX ICN KE 45301
JFK HKG CX 45191
LAX LHR BA 45053
LAX YYZ AC 43925
LAX MEX AM 42424
MIA HAV AA 42310
MIA BOG AV 42134
JFK ICN KE 42050
EWR LHR UA 42022
MIA GRU JJ 41789
FLL YYZ RV 41383
ATL YYZ DL 41283
LAX MNL PR 40737
HNL NRT JL 40108
MCO PTY CM 39171
0 0
Add a comment Improve this question Transcribed image text
Answer #1

First of all the function prototypes foes in the file routerecords.h, so that any other program can use it's function

##ifndef ROUTERECORD_H
#define ROUTERECORD_H

typedef struct RouteRecord RouteRecord;

int countRows(char* input_file_path);

struct RouteRecord* readInputFile(char* input_file_path, int N);

int findAirlinePassengerCount(struct RouteRecord* data, char* given_code, int N);

void writeResult(char* output_file_path, int result);
#endif

Then here goes the definition of the functions declared in the header file, written in routerecords.c

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

typedef struct RouteRecord{
        char origin[10];
        char destination[10];
        char airline_code[10];
        int passenger_count;
} RouteRecord;

int countRows(char* input_file_path) {
        FILE *file = fopen(input_file_path, "r");
        char buf[16];
        if(!file) {
                printf("can't open the file\n");
                return -1;
        }
        int count = 0;
        while(fgets(buf, 1024, file)) count++;
        fclose(file);
    return count;
}

struct RouteRecord* readInputFile(char* input_file_path, int N) {
        FILE *file = fopen(input_file_path, "r");
    char buf[16];
    char *token; 
    if(!file) {
        printf("can't open the file\n");
        return NULL;
    }
    struct RouteRecord *array;
    array = malloc(N * sizeof(struct RouteRecord));
    int i = 0;
    while(fgets(buf, 1024, file)) {
        buf[16] = '\0';
        token = strtok(buf, ",");
        strcpy((array + i)->origin,token);
        token = strtok(NULL, ",");
        strcpy((array + i)->destination, token);
        token = strtok(NULL, ",");
        strcpy((array + i)->airline_code, token);
        token = strtok(NULL, ",");
        int passenger_count;
        sscanf(token, "%d", &(array + i)->passenger_count);
        token = NULL;
        i++;
    }
    fclose(file);
    return array;
}

int findAirlinePassengerCount(struct RouteRecord* data, char* given_code, int N){
    int count = 0;
    for(int i=0; i<N; i++)
        if(!strcmp(data[i].airline_code, given_code)) count += data[i].passenger_count;
    return count;
}

void writeResult(char* output_file_path, int result){
    FILE *fptr;
    fptr = fopen(output_file_path, "w");
    if (fptr == NULL) {
        printf("Can't create/open the file");
        exit(1);
    }
    fprintf(fptr, "%d", result);
    fclose(fptr);
    return;
}

and then finally the main program which uses the functionality that is defined above, written in file a7.c as follows,

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

int main(int argc, char* argv[]) {
    if(argc < 4) {
        printf("Three argument required < inpit file name, output file name, Airline codes >\n");
        return 1;
    }
    int N = countRows(argv[1]);
    struct RouteRecord* data = readInputFile(argv[1], N);
    int result = findAirlinePassengerCount(data, argv[3], N);
    writeResult(argv[2], result);
    return 0;
}

finally a make file for the above program,

assignment-07-output.txt : routerecord.o
    ./routerecord.o temp_in.csv "assignment-07-output.txt" VS
routerecord.o : a7.c routerecord.c routerecord.h
    gcc a7.c routerecord.c -o routerecord.o

The functions are written in the header file routerecord.h shown above which has it's function definitions in file routerecord.c which then used through the file a7.c. the read function reads the csv and store each row that is a record and save it to the dynamically allocated array. The airline code can be searched through that array and there we can get the total number of passangers in the airline with the specific airline code/

Add a comment
Know the answer?
Add Answer to:
Consider the data file “assignment-07-input.csv. The data contains of 4 different things separated by comma –...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C program Consider a file that contains employee records, called "empstat.txt." The data for each...

    In C program Consider a file that contains employee records, called "empstat.txt." The data for each employee consist of the employee’s last name (up to 20 characters), employee id number (up to 11 characters), gross pay for the week (double), taxes deducted (double) for the week, and net pay (double) for the week. Create a struct to hold the employee information. Write a void function called writeEmpFile that prompts the user to enter last name, id number, gross pay and...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT