So the first part of the task is to create a structure in the following way:
- the trains id (integer)
- time ( two integers separated by a colon)
- 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!
>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}};