I have three different data structures:
-int-array with 144 entries called trains, each entry is a different trainID
-Station-array with 367 entries, each entry is a different Station-struct:
Code:
typedef struct
{
int id;
char name[51];
}Station;
-Schedule-array with 2548 entries, each is a different Schedule-entry struct:
Code:
typedef struct
{
int trainId;
int stationId;
int arrival; //in minutes, i.e. 2:15am = 135
int departure; //in minutes
}Schedule;
What I have to do, is to find all trains, which overtake an other train and ouput the trainId.
I got a hint from the teacher:
Code:
For all trains B
For all trains A
Find station C, where A departs and B later departs than A (that one where A departs as earliest)
If such exists:
For all stations D, where A arrives after C and B also arrives
If B is on station D earlier than A, then B is overtaker.
I think that should be some for-loops and some if-statements, but i don't get it.
I tried some different nested loops, but it never worked right. When i build it like the hint, how can i compare with the schedule, on which position do I need a loop which iterates over the schedule... Because the trains just have the id, also the station, I always have to look after entries in the schedule, I think this is my main problem.
Here some code from Main, I tried some different ways, i.e. instead of train-loops with schedule-loops, etc.
Code:
int main (int argc, char *argv[])
{
//lenght of all arrays
int lengthOfStations = countLines(argv[1]);
int lengthOfTrains = countLines(argv[2]);
int lengthOfSchedule = countLines(argv[3]);
int *B = readTrains(argv[2]);
int *A = readTrains(argv[2]);
Station *C = readStation(argv[1]);
Station *D = readStation(argv[1]);
Schedule *sched = readSchedule(argv[3]);
//qsort(sched, lengthOfSchedule, sizeof(Schedule), compare);
/* HERE I NEED THE NESTED LOOPS */
return 0;
}
The arguments are like above descripted, when I call readSchedule, then i.e. Schedule B is a Schedule-array with all 2548 schedule-elements, each is in this shape: "trainId stationId arrival departure" (i.e. 28281 6228 392 393). The expected output should be a list from all trains which overtake another train.
TY