Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user to enter a time (expressed in hours and minut

ID: 3761605 • Letter: W

Question

Write a program that asks the user to enter a time (expressed in hours and minutes, using the 24-hour clock). The program then displays the departure and arrival times for the flight whose departure time is closest to that entered by the user, using standard time.

Specifications:

The program should prompt the user to enter a time and pattern match against the ‘:’ character. After reading in the hours and minutes, calculate the time in minutes past midnight, and pass the time into a function that finds the flight whose departure time is closest to the desired_time (expressed in minutes since midnight). The function will store the departure and arrival times of the flight (also expressed in minutes since midnight) in variables pointed to by departure_time and arrival_time, respectively.

Use the following function prototype:

void find_closest_flight(int desired_time, int *departure_time, int *arrival_time);

Inside the function, define two constant integer arrays called departures andarrivals that store the departure times and arrival times of the airplane in minutes since midnight. Use a global constant to help convert the departure and arrival times to minutes past midnight. Use a while() loop and if-else statement to determine which element of the array contains the departure time closest to the desired_time. Then assign the appropriate departure and arrival times to the variables pointed to by the pointers departure_time and arrival_time, and exit the function.

Once the find_closest_flight() function returns back to the main function, use the information stored in the variables that were pointed to by departure_time and arrival_time in the function to back out the actual departure and arrival times. Since the times are in minutes past midnight, you must pull out the hour and minutes, convert from 24 time to standard time, and print out the appropriate results – with the appropriate number of digits.

For example, 13:15 is 13 x 60 + 15 = 795 minutes since midnight, which is closer to 12:47 pm (767 minutes since midnight) than to any of the other departure times.

~> hw22.o

Enter a 24-hour time: 13:15

Closest Departure/Arrival Times

Departure Time: 12:47 pm

Arrival Time: 3:00 pm

~>

~> hw22.o

Enter a 24-hour time: 23:59

Departure Time: 9:45 pm

Arrival Time: 11:58 pm

~>

~> hw22.o

Enter a 24-hour time: 3:12

Departure Time: 8:00 am

Arrival Time: 10:16 am

Explanation / Answer

#include <stdio.h>
#define NUM_FLIGHTS 8
struct flight_schedule
{
int dept_time;
int arrv_time;
};
const struct flight_schedule flight_times[NUM_FLIGHTS] ={{480, 616}, {583, 712}, {679, 811}, {767, 900}, {840, 968}, {945, 1075},{1140, 1280}, {1305, 1438}};
void find_closest_flight(int desired_time, int *departure_time, int *arrival_time);
int main(void)
{
int hour, minute, desired_flight_time, departure_time, arrival_time,
closest_hour, closest_minute;
for (;;)
{
printf(" Enter desired flight time (24-hour time, ""Press enter 0 to quit): ");
scanf("%2d:%2d", &hour, &minute);
if (hour == 0)
{
printf(" Exiting the program: bye. ");
break;
}
desired_flight_time = hour * 60 + minute;
find_closest_flight(desired_flight_time, &departure_time, &arrival_time);
if (departure_time > 780)
{
if (arrival_time > 780)
printf(" Closest departure time is %2d:%02d p.m., arriving, at %2d:%02d p.m. ",(departure_time / 60 - 12), (departure_time % 60),(arrival_time / 60 - 12), (arrival_time % 60));
else
printf(" Closest departure time is %2d:%02d p.m., arriving at %2d:%02d a.m. ",(departure_time / 60 - 12), (departure_time % 60),(arrival_time / 60), (arrival_time % 60));
}
else if (arrival_time > 780)
printf(" Closest departure time is %2d:%02d a.m., arriving at %2d:%02d p.m. ",(departure_time / 60), (departure_time % 60),(arrival_time / 60 - 12), (arrival_time % 60));
else
printf(" Closest departure time is %2d:%02d a.m., arriving at %2d:%02d a.m. ",(departure_time / 60), (departure_time % 60),(arrival_time / 60), (arrival_time % 60));
}
return 0;
}
void find_closest_flight(int desired_time, int *departure_time,int *arrival_time)
{
int i, min_diff;
if (flight_times[0].dept_time < desired_time)
min_diff = desired_time -flight_times[0].dept_time;
else
min_diff = flight_times[0].dept_time - desired_time;
(*departure_time) = flight_times[0].dept_time;
(*arrival_time) = flight_times[0].arrv_time;
for (i = 1; i < NUM_FLIGHTS; i++)
{
if (flight_times[i].dept_time < desired_time)
{
if (desired_time - flight_times[i].dept_time < min_diff)
{
min_diff = desired_time - flight_times[i].dept_time;
(*departure_time) = flight_times[i].dept_time;
(*arrival_time) = flight_times[i].arrv_time;
}
}
else
{
if (flight_times[i].dept_time - desired_time < min_diff)
{
min_diff = flight_times[i].dept_time - desired_time;
(*departure_time) = flight_times[i].dept_time;
(*arrival_time) = flight_times[i].arrv_time;
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote