Question

In: Computer Science

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

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.

You are free to use the following C libraries in this program ONLY:

stdio.h
stdlib.h
string.h

Solutions

Expert Solution

code

solution

//output

//copyable code

//header file

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

//define the variables

#define flightdataCODE_LENGTH 6

#define CITYCODE_LENGTH 3

#define MAXIMUM_number_flightdatas 5

#define DATABASENAME "database"

//create struct method

struct datetime

{

//declare the month,day,hour and minute

int mnth;

int day;

int hour;

int minute;

};

typedef struct datetime datetime_t;

//create flight method

struct flightdata

{    //declare the variables

char flightdataCode[flightdataCODE_LENGTH+1];

datetime_t departure_date;

char arrivalcity[CITYCODE_LENGTH+1];

datetime_t arrival_dt;

};

typedef struct flightdata flightdata_t;

//define function

void displaymenu (void);

int get_select();

void inputdata(int input);

void add_flightdata();

void display_flightdatas();

void data_Operation(char * o);

int Valid_Date(int mnth, int day, int hour, int minute);

flightdata_t flightdatas [MAXIMUM_number_flightdatas];

int number_flightdatas = 0;

//main method

int main(void)

{

displaymenu();

int select = get_select();

if(select == -1)

{

main();

}

inputdata(select);

return 0;

}

//display menu

void displaymenu (void)

{

printf("\n"

"1. add flightdata\n"

"2. print all flightdatas to a destination\n"

"3. save the flightdatas to the database \n"

"4. load the flightdatas \n"

"5. end program\n"

"please Enter choice (please enter number between 1-5)>\n");

}

//get select method

int get_select(){

int input = 0;

if (scanf("%d", &input) == 1)

{

if(input < 1 || input > 5)

{

            printf("Invalid \n");

            return -1;

        }

        else

        {

            return input;

        }

    }

    else

    {

        printf("Invalid \n");

        return -1;

    }

    return -1;

}

//input data function

void inputdata(int input)

{

    char * write = (char*)"w";

    char * read = (char*)"r";

    switch(input) {

      //case statement

        case 1: add_flightdata(); break;

        case 2: display_flightdatas(); break;

        case 3: data_Operation(write); break;

        case 4: data_Operation(read); break;

        case 5: exit(0); break;

    }

}

//add the flight method

void add_flightdata()

{  

    //check the condition

    if(number_flightdatas != MAXIMUM_number_flightdatas)

    {

        flightdata_t nflightdata;

        int mnth, day, hour, minute;

        int validator = -1;

        //while method process

        while(validator == -1)

        {

            printf("please Enter flightdata code>\n");

            scanf("%s", nflightdata.flightdataCode);

            if(strlen(nflightdata.flightdataCode) > 6)

            {

                printf("Invalid\n");

            }

            else

            {

                validator = 0;

            }

        }

        //display the data

printf("please Enter departure information for leaving SYD.\n");

validator = -1;

while(validator == -1)

{

printf("please Enter mnth, enter date, enter hour and enter minute >\n");

scanf("%d %d %d %d", &mnth, &day, &hour, &minute);

            validator = Valid_Date(mnth, day, hour, minute);

        }

        nflightdata.departure_date.mnth = mnth;

        nflightdata.departure_date.day = day;

        nflightdata.departure_date.hour = hour;

        nflightdata.departure_date.minute = minute;

        printf("please Enter arrival city code>\n");

        scanf("%s", nflightdata.arrivalcity);

        nflightdata.arrivalcity[3] = '\0';

        printf("please Enter arrival information.\n");

        validator = -1;

        while(validator == -1)

        {

printf("please Enter mnth, enter date, enter hour and enter minute \n");

scanf("%d %d %d %d", &mnth, &day, &hour, &minute);

            validator = Valid_Date(mnth, day, hour, minute);

        }

        nflightdata.arrival_dt.mnth = mnth;

        nflightdata.arrival_dt.day = day;

        nflightdata.arrival_dt.hour = hour;

        nflightdata.arrival_dt.minute = minute;

        flightdatas[number_flightdatas] = nflightdata;

        ++number_flightdatas;

    }

    else

    {

        printf("Could not found memory full\n");

    }

    main();

}

//display flight data

void display_flightdatas()

{

    char arrivalcity[CITYCODE_LENGTH+1];

    printf("please Enter arrivalcity code \n");

    scanf("%s", arrivalcity);

    if(strcmp(arrivalcity, "*" ) == 0)

    {

        if(number_flightdatas == 0)

        {

            printf("No flightdatas\n");

        }

        else{

            printf("flightdata Origin          Destination\n");

            printf("------ --------------- ---------------\n");

            int i = 0;

            while(i < number_flightdatas)

            {

                printf("%-6s ", flightdatas[i].flightdataCode);

printf("SYD %02d-%02d %02d:%02d ", flightdatas[i].departure_date.mnth,

flightdatas[i].departure_date.day, flightdatas[i].departure_date.hour,

flightdatas[i].departure_date.minute );

printf("%-3s %02d-%02d %02d:%02d\n", flightdatas[i].arrivalcity,

flightdatas[i].arrival_dt.mnth, flightdatas[i].arrival_dt.day,

flightdatas[i].arrival_dt.hour, flightdatas[i].arrival_dt.minute);

++i;

            }

      }

    }

    else

    {

        int i = 0;

        int matches = 0;

        while(i < number_flightdatas)

        {

            if(strcmp(arrivalcity, flightdatas[i].arrivalcity) == 0)

            {

                matches++ ;

            }

            ++i;

        }

        if(matches == 0)

        {

            printf("No flightdatas\n");

        }

        else{

            i = 0;

            printf("flightdata Origin          Destination\n");

            printf("------ --------------- ---------------\n");

            while(i < number_flightdatas)

            {

if(strcmp(arrivalcity, flightdatas[i].arrivalcity) == 0)

{

printf("%-6s ", flightdatas[i].flightdataCode);

printf("SYD %02d-%02d %02d:%02d ", flightdatas[i].departure_date.mnth, flightdatas[i].departure_date.day, flightdatas[i].departure_date.hour, flightdatas[i].departure_date.minute );

printf("%-3s %02d-%02d %02d:%02d\n", flightdatas[i].arrivalcity, flightdatas[i].arrival_dt.mnth, flightdatas[i].arrival_dt.day, flightdatas[i].arrival_dt.hour, flightdatas[i].arrival_dt.minute);

                }

                ++i;

            }

        }

    }

    main();

}

//valid the date

int Valid_Date(int mnth, int day1, int hour1, int min)

{

    if(mnth < 1 || mnth > 12 || day1 < 1 || day1 > 31 || hour1 < 0 || hour1 > 23 || min < 0 || min > 59)

    {

        printf("Invalid \n");

        return -1;

    }

    return 0;

}

//date operation

void data_Operation(char * o)

{

    FILE *fp;

    if(strcmp(o, "w" ) == 0)

    {

        fp = fopen(DATABASENAME, "w");

        int i = 0;

        while(i < number_flightdatas)

        {

fprintf(fp, "%s %02d %02d %02d %02d %s %02d %02d %02d %02d\n",

flightdatas[i].flightdataCode, flightdatas[i].departure_date.mnth,

flightdatas[i].departure_date.day, flightdatas[i].departure_date.hour,

flightdatas[i].departure_date.minute, flightdatas[i].arrivalcity,

flightdatas[i].arrival_dt.mnth, flightdatas[i].arrival_dt.day,

flightdatas[i].arrival_dt.hour, flightdatas[i].arrival_dt.minute);

            ++i;

        }

        fclose(fp);

    }

    //compare the function

    else if(strcmp(o, "r" ) == 0)

    {

        fp = fopen(DATABASENAME, "r");

        char currentline[33];

        if(!fp)

        {

flightdata_t databaseflightdatas[MAXIMUM_number_flightdatas];

int currentflightdatas = 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, " ");

                }

                flightdata_t nflightdata;

strcpy(nflightdata.flightdataCode, delimitedWord[0]);

                int charToInt = atoi(delimitedWord[1]);

                nflightdata.departure_date.mnth = charToInt;

                charToInt = atoi(delimitedWord[2]);

                nflightdata.departure_date.day = charToInt;

                charToInt = atoi(delimitedWord[3]);

                nflightdata.departure_date.hour = charToInt;

                charToInt = atoi(delimitedWord[4]);

                nflightdata.departure_date.minute = charToInt;

                strcpy(nflightdata.arrivalcity,delimitedWord[5]);

                charToInt = atoi(delimitedWord[6]);

                nflightdata.arrival_dt.mnth = charToInt;

                charToInt = atoi(delimitedWord[7]);

                nflightdata.arrival_dt.day = charToInt;

                charToInt = atoi(delimitedWord[8]);

                nflightdata.arrival_dt.hour = charToInt;

                charToInt = atoi(delimitedWord[9]);

                nflightdata.arrival_dt.minute = charToInt;

databaseflightdatas[currentflightdatas++] = nflightdata;

            }

            if(number_flightdatas != 0){

                memset(flightdatas, 0, sizeof(flightdatas));

            }

            int i = 0;

            while(i < currentflightdatas)

            {

                flightdatas[i] = databaseflightdatas[i];

                ++i;

            }

            number_flightdatas = currentflightdatas;

        }

        else

        {

            printf(" error\n");

        }

        fclose(fp);

    }

    main();

}



Related Solutions

Describe one way to collect AND use data in the ABA process. You can describe a...
Describe one way to collect AND use data in the ABA process. You can describe a real example if you have one.
Task 4: Develop Strategies for Improved Cultural Safety As part of this task, you are required...
Task 4: Develop Strategies for Improved Cultural Safety As part of this task, you are required to develop strategies for improved cultural safety when working with the Aboriginal and Torres Strait Islander people in your region (in relation to providing the information session). Activities you need to complete are provided in the questions below. Your answers must be 30-40 words each and specific to the Aboriginal and Torres Strait Islander people in your region and the information session being planned....
Develop your own questionnaire/survey to collect data on a two variables of interest. One variable of...
Develop your own questionnaire/survey to collect data on a two variables of interest. One variable of interest should be used to estimate a population mean (quantitative) and the other should be used to estimate a proportion (qualitative). I have to have 50 data values. Topic 2: Population proportion - For your second variable of interest (qualitative): ● Present the data you used ○ Use appropriate displays (graphs) of the data. (bar graph and pie chart) ○ Describe the data in...
Suppose that you are the manager of a casino and you collect data on one of...
Suppose that you are the manager of a casino and you collect data on one of your roulette tables. You find that among the last 10,000 bets, there were only 350 bets for which a red or black number did not win. On average, you expect the proportion of winning bets for black or red to equal p=18/19. a) Compute a 95% confidence interval for the proportion of winning black or red bets. b) Conduct a two-sided hypothesis test (α=0.05)for...
-Name & explain a task that can be achieved in Data Mining process in all of...
-Name & explain a task that can be achieved in Data Mining process in all of following fields and -why it is considered as data mining: (a) Manufacturing (b) Medical/Pharmacology (c) Physics/Astronomy
8. What additional data would you like to collect?
8. What additional data would you like to collect?
Your task is to develop quantified Risk models of two Risks of interest to you. This...
Your task is to develop quantified Risk models of two Risks of interest to you. This is an academic exercise and its purpose is to make you familiar with the process of quantified Risk modelling and to show me you understand the theory and the practice. a) Write up each Risk as a case study that explains the Risk in detail and describes the proposed control measures. b) Estimate/judge the values that could realistically be given to the various parameters...
Your task is to develop quantified Risk models of two Risks of interest to you. This...
Your task is to develop quantified Risk models of two Risks of interest to you. This is an academic exercise and its purpose is to make you familiar with the process of quantified Risk modelling and to show me you understand the theory and the practice. a) Write up each Risk as a case study that explains the Risk in detail and describes the proposed control measures.
Honda uses statistical process control to monitor and improve a variety of processes. Employees collect data,...
Honda uses statistical process control to monitor and improve a variety of processes. Employees collect data, and then analyze it using statistical process control charts. They can make adjustments if needed. The statistical process control charts enable workers to understand precisely how processes are working and anticipate problems before they occur. One interesting use is in monitoring the effort required to close a door. Honda monitors the speed of the door required to make it latch by using a special...
To complete this task you are required to design an information system for Fashion clothing store...
To complete this task you are required to design an information system for Fashion clothing store to assist with their business. You have discussed Porter’s Value Chain in class and you should understand the Primary and support activities within businesses. For this task you need to concentrate on Marketing and Sales only. The development of your professional skills includes researching information systems to assist with organisational issues that are encountered in contemporary business. You will be learning important ‘agile’ skills...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT