Computer Science, asked by subito388, 7 months ago

Define a structure data type TRAIN_INFO. The type contain Train No.: integer type Train name: string Departure Time: aggregate type TIME Arrival Time: aggregate type TIME Start station: string End station: string The structure type Time contains two integer members: hour and minute. Maintain a train timetable and implement the following operations:
(i) List all the trains (sorted according to train number) that depart from a particular section.
(ii) List all the trains that depart from a particular station at a particular time.
(iii) List all he trains that depart from a particular station within the next one hour of a given time.
(iv) List all the trains between a pair of start station and end station.

Answers

Answered by ujjwalawasthi96
1

Answer:

........

____________________________

Answered by tiwariakdi
0

Sure, here's an implementation of the structure data type TRAIN_INFO in C:

struct Time {

   int hour;

   int minute;

};

struct TRAIN_INFO {

   int train_no;

   char train_name[50];

   struct Time departure_time;

   struct Time arrival_time;

   char start_station[50];

   char end_station[50];

};

To maintain a train timetable, you could create an array of TRAIN_INFO structures:

go

struct TRAIN_INFO timetable[100];

int num_trains = 0; // number of trains in timetable

Now, let's implement the operations you requested:

(i) To list all the trains (sorted according to train number) that depart from a particular section, you could loop through the timetable and check if the start station matches the given section:

c

void list_trains_from_section(char* section) {

   // loop through timetable and print trains that depart from section

   for (int i = 0; i < num_trains; i++) {

       if (strcmp(timetable[i].start_station, section) == 0) {

           printf("Train No.: %d, Train Name: %s, Departure Time: %02d:%02d\n", timetable[i].train_no, timetable[i].train_name, timetable[i].departure_time.hour, timetable[i].departure_time.minute);

       }

   }

}

(ii) To list all the trains that depart from a particular station at a particular time, you could use the same loop as above, but check both the start station and departure time:

c

void list_trains_from_station_and_time(char* station, struct Time time) {

   // loop through timetable and print trains that depart from station at given time

   for (int i = 0; i < num_trains; i++) {

       if (strcmp(timetable[i].start_station, station) == 0 && timetable[i].departure_time.hour == time.hour && timetable[i].departure_time.minute == time.minute) {

           printf("Train No.: %d, Train Name: %s, Departure Time: %02d:%02d\n", timetable[i].train_no, timetable[i].train_name, timetable[i].departure_time.hour, timetable[i].departure_time.minute);

       }

   }

}

(iii) To list all the trains that depart from a particular station within the next one hour of a given time, you could again use the same loop, but add an additional check for the departure time being within one hour of the given time:

c

void list_trains_from_station_within_one_hour(char* station, struct Time time) {

   // loop through timetable and print trains that depart from station within one hour of given time

   for (int i = 0; i < num_trains; i++) {

       if (strcmp(timetable[i].start_station, station) == 0) {

           int time_diff = (timetable[i].departure_time.hour - time.hour) * 60 + (timetable[i].departure_time.minute - time.minute);

           if (time_diff >= 0 && time_diff <= 60) {

               printf("Train No.: %d, Train Name: %s, Departure Time: %02d:%02d\n", timetable[i].train_no, timetable[i].train_name, timetable[i].departure_time.hour, timetable[i].departure_time.minute);

           }

       }

   }

}

(iv) To list all the trains between a pair of start station and end station, you could again use the same loop, but check both the start and end stations:

java

void list_trains_between_stations(char* start_station, char* end_station)

For similar question on Data type

https://brainly.in/question/42952434

#SPJ2

Similar questions