Code:
#include <stdio.h>
#include <stdlib.h>
void find_closest_flight(int desired_time, int *departure_time, int *arrival_time);
int main()
{
int hour = 0;
int minute = 0;
// Departure times in minutes after midnight
// int departure_times[] =
// {8*60, 9*60+43, 11*60+19, 12*60+47,
// 14*60, 15*60+45, 19*60, 21*60+45};
// Arrival times in minutes after midnight
// int arrival_times[] =
// { 10*60+16, 11*60+52, 13*60+31, 15*60,
// 16*60+8, 17*60+55, 21*60+20, 23*60+58};
// Length of the array.
//int length = sizeof(arrival_times) / sizeof(arrival_times[0]);
int departure_hour, departure_min;
char departure_halfday;
int arrival_hour, arrival_min;
char arrival_halfday;
// int i;
// int time_in_minutes;
// int difference1, difference2, difference3;
// int min_difference, best_index;
printf ("\nEnter a 24-hour time: ");
scanf("%d:%d", &hour, &minute);
printf ("You entered %2d:%02d\n", hour, minute);
// Get user's time in minutes after midnight.
//time_in_minutes = hour*60 + minute;
//Look through the departure time array and find the value that
//is closest to the time entered by the user.
//min_difference = 100000;
// for (i = 0; i < length; i++)
// {
//Difference between user's time and flight time today.
// difference1 = abs(time_in_minutes - departure_times[i]);
// if (difference1 < min_difference)
// {
// min_difference = difference1;
// best_index = i;
// }
// Difference between user's time and flight time previous day.
// difference2 = abs(time_in_minutes - (departure_times[i] - 60*24));
// if (difference2 < min_difference)
// {
// min_difference = difference2;
// best_index = i;
// }
//Difference between user's time and flight time next day.
// difference3 = abs(time_in_minutes - (departure_times[i] + 60*24));
// if (difference3 < min_difference)
// {
// min_difference = difference3;
// best_index = i;
// }
// }
// We have determined the closes departure time in minutes after midnight.
// Now output it as a time of day.
// i = best_index;
// departure_hour = departure_times[i] / 60;
// departure_min = departure_times[i] % 60;
// departure_halfday = 'a';
// arrival_hour = arrival_times[i] / 60;
// arrival_min = arrival_times[i] % 60;
// arrival_halfday = 'a';
if (departure_hour >= 12)
{
// Departure is at noon or later.
departure_halfday = 'p';
if (departure_hour > 12)
{
departure_hour -= 12;
}
}
else
{
// Departure is before noon.
if (departure_hour < 1)
{
// 30 minutes after midnight should be 12:30 A.M.
departure_hour += 12;
}
}
if (arrival_hour >= 12)
{
// Arrival time is at noon or later.
arrival_halfday = 'p';
if (arrival_hour > 12)
{
arrival_hour -= 12;
}
}
else
{
// Arrival time is before noon.
if (arrival_hour < 1)
{
arrival_hour += 12;
}
}
printf ("closest departure time is %2d:%02d %c.m., ",
departure_hour, departure_min, departure_halfday);
printf("arriving at %2d:%02d %c.m.",
arrival_hour, arrival_min, arrival_halfday);
getchar();
getchar();
return 0;
}
void find_closest_flight(int desired_time, int *departure_time, int *arrival_time)
{
int hour = 0;
int minute = 0;
//int desired_time;
//int departure_time;
//int arrival_time;
departure_time = &departure_time;
arrival_time = &arrival_time;
int departure_hour, departure_min;
char departure_halfday;
int arrival_hour, arrival_min;
char arrival_halfday;
int i;
int time_in_minutes;
int difference1, difference2, difference3;
int min_difference, best_index;
// Departure times in minutes after midnight
int departure_times[] =
{8*60, 9*60+43, 11*60+19, 12*60+47,
14*60, 15*60+45, 19*60, 21*60+45};
// Arrival times in minutes after midnight
int arrival_times[] =
{ 10*60+16, 11*60+52, 13*60+31, 15*60,
16*60+8, 17*60+55, 21*60+20, 23*60+58};
i = best_index;
departure_hour = departure_times[i] / 60;
departure_min = departure_times[i] % 60;
departure_halfday = 'a';
arrival_hour = arrival_times[i] / 60;
arrival_min = arrival_times[i] % 60;
arrival_halfday = 'a';
int length = sizeof(arrival_times) / sizeof(arrival_times[0]);
// Get user's time in minutes after midnight.
time_in_minutes = hour*60 + minute;
//Look through the departure time array and find the value that
//is closest to the time entered by the user.
min_difference = 100000;
for (i = 0; i < length; i++)
{
// Difference between user's time and flight time today.
difference1 = abs(time_in_minutes - departure_times[i]);
if (difference1 < min_difference)
{
min_difference = difference1;
best_index = i;
}
// Difference between user's time and flight time previous day.
difference2 = abs(time_in_minutes - (departure_times[i] - 60*24));
if (difference2 < min_difference)
{
min_difference = difference2;
best_index = i;
}
// Difference between user's time and flight time next day.
difference3 = abs(time_in_minutes - (departure_times[i] + 60*24));
if (difference3 < min_difference)
{
min_difference = difference3;
best_index = i;
}
return;
}