Member Avatar for kyeong

Use the given void function. This function will find the flight whose departure time is closest to desired_time(expressed in minutes since midnight). It will store the departure and arrival times of this flight (minutes also) in the variables pointed to by departure_time and arrival_time, respectively.

Here's what I've gotten so far.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

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

int main(void)
{
    int hour = 0;
    int minute = 0;
    int time_in_minutes, depart, arrival;
    // Departure times in minutes after midnight
    
    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;

	find_closest(time_in_minutes, &depart, &arrival);
	
	printf("Closest departure time is %d......arriving at %d", depart, arrival);
	
    getchar();
    getchar();
    return 0;
}

void find_closest(int desired_time, int *departure_time, int *arrival_time)
{
    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 i;
    int difference1, difference2, difference3;
    int min_difference, best_index;
	
    int departure_hour, departure_min;
    char departure_halfday;
    int arrival_hour, arrival_min;
    char arrival_halfday;

    // 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(desired_time - 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(desired_time - (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(desired_time - (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;
        }
    }
}

First of all, am I doing this correctly? Going into the right direction? Also, how would I go about, I guess the term is storing, storing the variables in the function to the pointers so the main function will read it and print it out correctly? I'm just stuck right now.

Thanks.

What's departure_time and arrival_time doing in the function? You've passed them as arguments in the function, but they aren't being made to point to anything. Better thing to do would be to assign them to the variable pointed to by best_index in both departure_times[] and arrival_times[] and do all the conversion to hours and minutes in the main function, or have another function which does the conversion and call it from the main function once you get the best departure and arrival times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.