Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,161 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,745 people online right now. Registration is fast and FREE... Join Now!




Determine the closest departure time

 
Reply to this topicStart new topic

Determine the closest departure time

kyeong
post 7 Oct, 2008 - 01:42 PM
Post #1


New D.I.C Head

*
Joined: 12 Feb, 2008
Posts: 22

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.

cpp
#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);


while (i > 0)
{
hour = hour * 60;
minute = minute + hour;

// if (minute > 0)
// {
// printf("%d\n", minute);

// }

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.

EDIT: 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.

This post has been edited by kyeong: 7 Oct, 2008 - 02:02 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 02:20PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month