Thread: Determine the closest departure time

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    14

    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.

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    14
    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;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    14
    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?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No you need, as your book says, abs(small).

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #10
    Registered User
    Join Date
    May 2008
    Posts
    14
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM