# Determine the closest departure time

• 10-07-2008
Kyeong
Determine the closest departure time
I have to use arrays for this projects and I only understand the basics of arrays. I have no clue how to implement it into the loop and am stuck right now. Oh yeah, I did a search and couldn't find a similar topic.

I am given a set of departure and arrival times. A user is suppose to enter a 24-hour time and then the program displays the closest departure time out of the given times that are available.

This is an example output.

Quote:

Enter a 24-hour time: 13:15
You entered 13:15
Closest departure time is 12:47 p.m., arriving at 3:00 p.m.
This is the code I have gotten so far. It's only the beginning so far.

Code:

```#include <stdio.h> #include <stdlib.h> int main() {     int departure_times[] =                 {8*60, 9*60+43, 11*60+19, 12*60+47,         14*60, 15*60+45, 19*60, 21*60+45};     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};                       int i, hour, minute;                 printf("Enter a 24-hour time: ");         scanf("%d:%d", &hour, &minute);         printf("You entered %d:%d\n", hour, minute);             for (i = 0; i < minute; i++)     {                 hour = hour * 60;                 minute = minute + hour;                                 i++         }                     getchar();     getchar();     return 0; }```
The problem I'm having is implementing the array inside the loop. I'm thinking about using a for loop instead of a while loop. The program is suppose to search inside this loop to find the closest departure time. I would like to ask for some tips to get me started on the loop, or examples.

Thanks.

Almost forgot, I am suppose to use the abs() function in this program, but I also have no clue on how to implement this. It's suppose to be used with what the user inputs as time since it could be before or after the closest time.
• 10-07-2008
tabstop
You don't need to implement the array inside the loop, since "implement the array" is a nonsensical phrase, and you don't need to implement abs(), since that's part of C. I assume the word you're looking for is "use".

So do you know how, given two times, how to find the difference in times (regardless of which one is later)? Do you know how, given a list of numbers to find the smallest? (This is what you should have searched on, I bet.) Do you know how to "walk through" an array, processing each element in turn?
• 10-07-2008
Kyeong
Quote:

Originally Posted by tabstop
You don't need to implement the array inside the loop, since "implement the array" is a nonsensical phrase, and you don't need to implement abs(), since that's part of C. I assume the word you're looking for is "use".

So do you know how, given two times, how to find the difference in times (regardless of which one is later)? Do you know how, given a list of numbers to find the smallest? (This is what you should have searched on, I bet.) Do you know how to "walk through" an array, processing each element in turn?

Yes.

Sort of, I read through this thread and kind of got the idea of how to get find the smallest.

Nope. Still iffy with the arrays.
• 10-07-2008
tabstop
Consider this code to print out an array one by one on each line:
Code:

```for (i = 0; i < ARRAY_SIZE; i++) {     printf("%d\n", array[i]); }```
The bit that your for-loop advances (I called it i here) is what allows you to go through each slot in your array.

Now instead of printing, you have to do some calculating and comparing, but you'll need to calculate and compare for every slot in the array.
• 10-07-2008
Kyeong
Ok, I'm starting to understand this now, at least I think I am. I calculated each slot in the array to determine the smallest difference, but got a negative number. Now, I'm trying to use the abs() function, it's not working. The error message I get is "too few arguments to function 'abs'." How do I fix this problem?

Code:

```#include <stdio.h> #include <stdlib.h> int main() {     int departure_times[] =                 {8*60, 9*60+43, 11*60+19, 12*60+47,         14*60, 15*60+45, 19*60, 21*60+45};     // going to use this array later     // 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};                       int i, hours, minutes, small;     int length = sizeof(departure_times) / sizeof(departure_times[0]);         printf("Enter a 24-hour time: ");         scanf("%d:%d", &hours, &minutes);         printf("You entered %d:%d\n", hours, minutes);             for (i = 0; i < length; i++)     {                 hours = hours * 60;                 minutes = minutes + hours;                 departure_times[i] -= minutes;                                 for (small = departure_times[0], i =1; i < length; ++i)                 {                         if (departure_times[i] < small)                         {                                                                 small = departure_times[i];                         }                 }                 printf("Minutes: %d\n", small); //going to be erased later         }                     getchar();     getchar();     return 0; }```
• 10-07-2008
tabstop
I don't see abs anywhere in your code. But all it does is return the absolute value of whatever argument you give it, like abs(-6) is 6.
• 10-07-2008
Kyeong
In my textbook, the example given is: int abs(int j). So I tried int abs(int small), but that didn't work because it's an invalid assignment. So I tried "small = abs();" in the if statement, but that didn't work either. Maybe I'm just using the wrong variable?
• 10-07-2008
tabstop
No you need, as your book says, abs(small).
• 10-07-2008
nonoob
I don't know if absolute value is the way to go. Closest departure time? Doesn't that mean someone might be planning to take a trip using some transit method? So any earlier times than the user's time is immaterial. You can't go back in time to board an earlier train. Just toss those departure and arrival time slots away. Just determine nearest positive values. My 2 cents.

Maybe I'm not understanding the problem. Is it supposed to be real-life? Or some forensic investigation "which train would the suspect have most likely boarded to put him at the scene of the crime" - in which case closest might include the fudge factor.
• 10-07-2008
Kyeong
It doesn't matter if it's back in time or forward. Whichever is the closest, that will be displayed. At least that's how the sample output looked like.