Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How should I build-up this structure?

So the first part of the task is to create a structure in the following way:

  1. the trains id (integer)
  2. time ( two integers separated by a colon)
  3. whether they will arrive or start (0 for arrival and 1 for start)

I have no problem with the 1 and 3. But I have no clue how should I do the 2 because of the column.
Any ideas would be appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are many ways. Here is one:

typedef struct {
    int id;
    int hr;//hr/min can be arranged in a string separated by a space (or colon)
    int min;
    bool direction;//where arriving == false, departing == true
}train_s;

train_s train;

This can be used to create a single instance or an array…

   int main(void)
   {
       train_s train = {0};//single instance

       train.id = 12345;
       sprintf(train.time, "%s", "01:35");
       train.direction = true;
       ...
   

Or a string can be used for time that would allow room for two values in two columns….

typedef struct {
    int id;
    char time[6];//eg "12:00"
    bool direction;//where arriving == false, departing == true
}train_s;     

....

int main(void) 
{     //array of instances...
      train_s train[5] = {{12345, "12:00", 0},
                          {23452, "13:45", 1},
                          {67893, "15:38", 1},
                          {67801, "01:30", 0},
                          {73356, "02:45", 1}};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading