Question

Description Data Engineers regularly collect, process and store data. In this task you will develop a...

Description

Data Engineers regularly collect, process and store data. In this task you will develop a deeper understanding of how C programming language can be used for collecting, processing and storing data. In this assignment you get the opportunity to build an interactive program that can manage the list of flights departing Sydney Airport.

The list is stored as an array of flight_t type structures

flight_t flights [MAX_NUM_FLIGHTS];

The flight_t is a structure typedef for struct flight. The struct flight contains the following fields

  • flightcode - array of MAX_FLIGHTCODE_LEN+1 chars (string)
  • departure_dt - a structure of date_time_t type as defined below
  • arrival_city - array of MAX_CITYCODE_LEN+1 chars (string)
  • arrival_dt - a structure of date_time_t type as defined below

Note that we now have a struct nested within a struct. The date_time_t is a structure typedef for struct date_time. The struct date_time contains the following fields,

  • month - integer between 1 and 12 (inclusive)
  • day - integer between 1 and 31 (inclusive)
  • hour - integer between 0 and 23 (inclusive)
  • minute - integer between 0 and 59 (inclusive)

Your program interacts with the nested struct array in your memory (RAM) and simple database file in your hard disk. It should provide the following features:

1. add a flight

Add a new flight to the flights through the terminal. You should collect the input by asking multiple questions from the user.

Enter flight code>
Enter departure info for the flight leaving SYD.
Enter month, date, hour and minute separated by spaces>
Enter arrival city code>
Enter arrival info.
Enter month, date, hour and minute separated by spaces>

2. display all flights to a destination

Prompt the following question

Enter arrival city code or enter * to show all destinations>

The user may enter the abbreviation of MAX_CITYCODE_LEN characters for the arrival city. The program should display all flights to the requested destination. If the user input is *, display all flights.

The display format should is as follows.

Flight Origin          Destination
------ --------------- ---------------
VA1    SYD 11-26 09:54 LAX 11-26 18:26

Pay attention to the strict formatting guide:

  • Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most.
  • Origin and Destination
  • City - left aligned, MAX_CITYCODE_LEN (i.e. 3) chars at most.
  • Month, day, hour, minute - two digits with leading zeros

3. save the flights to the database file

Save the flights in the hard disk as a binary/text file named database. You may use your own format to save the data. You should overwrite if database file already exists.

4. load the flights from the database file

Read the database file and put the data into flights. You may only read the data files created by your own program. You should overwrite the flights array you had in memory when loading from the file.

5. exit the program

Exit the interactive program.

Careless Users

Your program may assume that the input data type is always the expected type i.e. when the program expects an integer the user must enter an integer. However, a careless user may enter an input that is outside the expected range (but still of the expected data type). Your program is expected to handle careless users. e.g.

Enter choice (number between 1-5)>
-1
Invalid choice

Or a careless user may try to add 365 as the month (month should be between 1 and 12). Or try to add a flight to the flights array when it already contains MAX_NUM_FLIGHTS flights, etc.

Run the sample executable to futher understand the expected behaviour.

Check the formatting of the flightcode

WARNING: Attempting this feature is recommended only for advanced students who enjoy a small challenge. You may need to do your own research, but more than that you may have to be creative. By using incorrect techniques you could very well introduce more bugs in your code and it could be time consuming. The special techniques required for this purpose will not be assessed in the final exam.

Your program should be able to check the format of the flightcode. The first two characters of the flightcode should be uppercase letters (A-Z) representing the airline. The rest of the flightcode should be numerals (0-9) representing the flight number. There must be 1-4 numerals as the flight number part of the flightcode. No spaces in the flightcode.

Run the sample executable to further understand the expected behaviour.

The database file

It is up to you to create your own data storage format for the database file. Your program should be able to read the database that was created by itself. You can create the database as a text or binary file.

You do NOT need to be able to create a database identical to the database of the sample executable. You do NOT need to be able to read the database of the sample executable.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

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

#define MAX_FLIGHTCODE_LEN 6
#define MAX_CITYCODE_LEN 3
#define MAX_NUM_FLIGHTS 5
#define DB_NAME "database"

struct date_time
{
    int month;
    int day;
    int hour;
    int minute;
};
typedef struct date_time date_time_t;

struct flight
{
    char flightCode[MAX_FLIGHTCODE_LEN+1];
    date_time_t departure_dt;
    char arrival_city[MAX_CITYCODE_LEN+1];
    date_time_t arrival_dt;
};
typedef struct flight flight_t;
void print_menu (void);
int getSelection();
void handleInput(int input);
void addFlight();
void printFlights();
void dataOperation(char * o);
int isValidDate(int month, int day, int hour, int minute);

flight_t flights [MAX_NUM_FLIGHTS];
int numFlights = 0;

int main(void)
{
    print_menu();
    int selection = getSelection();
    if(selection == -1)
    {
        main();
    }
    handleInput(selection);
    return 0;
}
void print_menu (void)
{
    printf("\n"
           "1. add a flight\n"
           "2. display all flights to a destination\n"
           "3. save the flights to the database file\n"
           "4. load the flights from the database file\n"
           "5. exit the program\n"
           "Enter choice (number between 1-5)>\n");
}

int getSelection(){
    int input = 0;

    if (scanf("%d", &input) == 1)
    {
        if(input < 1 || input > 5)
        {
            printf("Invalid choice\n");
            return -1;
        }
        else
        {
            return input;
        }
    }
    else
    {
        printf("Invalid input, not a number\n");
        return -1;
    }
    return -1;
}

void handleInput(int input)
{
    char * write = "w";
    char * read = "r";
    switch(input) {
        case 1: addFlight(); break;
        case 2: printFlights(); break;
        case 3: dataOperation(write); break;
        case 4: dataOperation(read); break;
        case 5: exit(0); break;
    }
}

void addFlight()
{
    if(numFlights != MAX_NUM_FLIGHTS)
    {
        flight_t newFlight;
        int month, day, hour, minute;
        int validator = -1;
        while(validator == -1)
        {
            printf("Enter flight code>\n");
            scanf("%s", newFlight.flightCode);
            if(strlen(newFlight.flightCode) > 6)
            {
                printf("Invalid input\n");
            }
            else
            {
                validator = 0;
            }
        }
        printf("Enter departure info for the flight leaving SYD.\n");
        validator = -1;
        while(validator == -1)
        {
            printf("Enter month, date, hour and minute separated by spaces>\n");
            scanf("%d %d %d %d", &month, &day, &hour, &minute);
            validator = isValidDate(month, day, hour, minute);
        }
        newFlight.departure_dt.month = month;
        newFlight.departure_dt.day = day;
        newFlight.departure_dt.hour = hour;
        newFlight.departure_dt.minute = minute;

        printf("Enter arrival city code>\n");
        scanf("%s", newFlight.arrival_city);
        newFlight.arrival_city[3] = '\0';
        printf("Enter arrival info.\n");
        validator = -1;
        while(validator == -1)
        {

            printf("Enter month, date, hour and minute separated by spaces>\n");
            scanf("%d %d %d %d", &month, &day, &hour, &minute);
            validator = isValidDate(month, day, hour, minute);
        }
        newFlight.arrival_dt.month = month;
        newFlight.arrival_dt.day = day;
        newFlight.arrival_dt.hour = hour;
        newFlight.arrival_dt.minute = minute;

        flights[numFlights] = newFlight;
        ++numFlights;
    }
    else
    {
        printf("Cannot add more flights - memory full\n");
    }
    main();
}

void printFlights()
{
    char arrival_city[MAX_CITYCODE_LEN+1];
    printf("Enter arrival city code or enter * to show all destinations>\n");
    scanf("%s", arrival_city);

    if(strcmp(arrival_city, "*" ) == 0)
    {
        if(numFlights == 0)
        {
            printf("No flights\n");
        }
        else{
            printf("Flight Origin          Destination\n");
            printf("------ --------------- ---------------\n");
            int i = 0;
            while(i < numFlights)
            {
                printf("%-6s ", flights[i].flightCode);
                printf("SYD %02d-%02d %02d:%02d ", flights[i].departure_dt.month,
                       flights[i].departure_dt.day, flights[i].departure_dt.hour,
                       flights[i].departure_dt.minute );

                printf("%-3s %02d-%02d %02d:%02d\n", flights[i].arrival_city,
                       flights[i].arrival_dt.month, flights[i].arrival_dt.day,
                       flights[i].arrival_dt.hour, flights[i].arrival_dt.minute);
                ++i;
            }
        }
    }
    else
    {
        int i = 0;
        int matches = 0;
        while(i < numFlights)
        {
            if(strcmp(arrival_city, flights[i].arrival_city) == 0)
            {
                matches++ ;
            }
            ++i;
        }

        if(matches == 0)
        {
            printf("No flights\n");
        }
        else{
            i = 0;
            printf("Flight Origin          Destination\n");
            printf("------ --------------- ---------------\n");
            while(i < numFlights)
            {
                if(strcmp(arrival_city, flights[i].arrival_city) == 0)
                {
                    printf("%-6s ", flights[i].flightCode);
                    printf("SYD %02d-%02d %02d:%02d ", flights[i].departure_dt.month, flights[i].departure_dt.day, flights[i].departure_dt.hour, flights[i].departure_dt.minute );
                    printf("%-3s %02d-%02d %02d:%02d\n", flights[i].arrival_city, flights[i].arrival_dt.month, flights[i].arrival_dt.day, flights[i].arrival_dt.hour, flights[i].arrival_dt.minute);
                }
                ++i;
            }
        }
    }
    main();
}

int isValidDate(int month, int day, int hour, int minute)
{
    if(month < 1 || month > 12 || day < 1 || day > 31 || hour < 0 || hour > 23 || minute < 0 || minute > 59)
    {
        printf("Invalid input\n");
        return -1;
    }
    return 0;
}


void dataOperation(char * o)
{
    FILE *fp;
    if(strcmp(o, "w" ) == 0)
    {
        fp = fopen(DB_NAME, "w");
        int i = 0;

        while(i < numFlights)
        {
            fprintf(fp, "%s %02d %02d %02d %02d %s %02d %02d %02d %02d\n",
                    flights[i].flightCode, flights[i].departure_dt.month,
                    flights[i].departure_dt.day, flights[i].departure_dt.hour,
                    flights[i].departure_dt.minute, flights[i].arrival_city,
                    flights[i].arrival_dt.month, flights[i].arrival_dt.day,
                    flights[i].arrival_dt.hour, flights[i].arrival_dt.minute);
            ++i;
        }
        fclose(fp);
    }
    else if(strcmp(o, "r" ) == 0)
    {
        fp = fopen(DB_NAME, "r");
        char currentline[33];
        if(!fp)
        {
            flight_t databaseFlights[MAX_NUM_FLIGHTS];
            int currentFlights = 0;
            while (fgets(currentline, sizeof(currentline), fp) != NULL) {
                int tracker = 0;
                char * parser = strtok(currentline, " ");
                char *delimitedWord[50];
                while(parser != NULL)
                {
                    delimitedWord[tracker++] = parser;
                    parser = strtok (NULL, " ");
                }
                flight_t newFlight;
                strcpy(newFlight.flightCode, delimitedWord[0]);

                int charToInt = atoi(delimitedWord[1]);
                newFlight.departure_dt.month = charToInt;

                charToInt = atoi(delimitedWord[2]);
                newFlight.departure_dt.day = charToInt;

                charToInt = atoi(delimitedWord[3]);
                newFlight.departure_dt.hour = charToInt;

                charToInt = atoi(delimitedWord[4]);
                newFlight.departure_dt.minute = charToInt;

                strcpy(newFlight.arrival_city,delimitedWord[5]);

                charToInt = atoi(delimitedWord[6]);
                newFlight.arrival_dt.month = charToInt;

                charToInt = atoi(delimitedWord[7]);
                newFlight.arrival_dt.day = charToInt;

                charToInt = atoi(delimitedWord[8]);
                newFlight.arrival_dt.hour = charToInt;

                charToInt = atoi(delimitedWord[9]);
                newFlight.arrival_dt.minute = charToInt;
                databaseFlights[currentFlights++] = newFlight;
            }
            if(numFlights != 0){
                memset(flights, 0, sizeof(flights));
            }
            int i = 0;
            while(i < currentFlights)
            {
                flights[i] = databaseFlights[i];
                ++i;
            }
            numFlights = currentFlights;
        }
        else
        {
            printf("Read error\n");
        }
        fclose(fp);
    }
    main();
}



Add a comment
Know the answer?
Add Answer to:
Description Data Engineers regularly collect, process and store data. In this task you will develop a...
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
  • /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info...

    /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info for the flight leaving SYD. Enter month, date, hour and minute separated by spaces> Enter arrival city code> Enter arrival info. Enter month, date, hour and minute separated by spaces> It should do all of this: 1-Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most. 2-City - left aligned, MAX_CITYCODE_LEN 3 chars at most . For example( VA1 or LAX ) 3- Month,...

  • Project Description Complete the Film Database program described below. This program allows users to store a...

    Project Description Complete the Film Database program described below. This program allows users to store a list of their favorite films. To build this program, you will need to create two classes which are used in the program’s main function. The “Film” class will store information about a single movie or TV series. The “FilmCollection” class will store a set of films, and includes functions which allow the user to add films to the list and view its contents. IMPORTANT:...

  • This is in Java The Problem: A common task in computing is to take data and...

    This is in Java The Problem: A common task in computing is to take data and apply changes (called transactions) to the data, then saving the updated data. This is the type of program you will write.             You will read a file of flight reservation data to be loaded into an array with a max size of 20. A second file will hold the transactions that will be applied to the data that has been stored in the array....

  • Hello. I am using a Java program, which is console-baed. Here is my question. Thank you....

    Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...

  • Introduction: In this lab, you will write a MIPS program to read in (up to) 50...

    Introduction: In this lab, you will write a MIPS program to read in (up to) 50 integer values from the user, store them in an array, print out the amay, one number per line, reverse the elements in the array and finally print out the elements in the just-reversed) array. Feel free to do this lab and all assembly programming labs) in Windows. You must use MARS Getting started: l. In MARS, create a new assembly file with the name...

  • Description For this assignment, you will maintain an array of bank accounts. You will declare a...

    Description For this assignment, you will maintain an array of bank accounts. You will declare a struct type called bankAcct and have an array of bankAcct with maximum size of 20, the members of the struct are shown below struct bankAcct { string first , last ; double amount ; long acctNo ; short pin ; }; Each member is described below • string first stores the user’s first name • string last stores the user’s last name • double...

  • In this assignment, you will write one (1) medium size C program. The program needs to...

    In this assignment, you will write one (1) medium size C program. The program needs to be structured using multiple functions, i.e., you are required to organize your code into distinct logical units. The following set of instructions provide the specific requirements for the program. Make sure to test thoroughly before submitting. Write   a   program,   named   program1.c,   that   reads   and   processes   employee   records   (data   about   an   employee).   Each   employee   record   must   be   stored   using   a   struct   that   contains   the   following  ...

  • Question: C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to prov......

    Question: C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to prov... C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to provide practice programming using structs and pointers. Your program will accept user input and store it in a dynamic array of structs. First, you will define a structure to describe a item to be bought. Your program will prompt the user for the initial size of the array. It will...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

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