Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4121630
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:22:34+00:00 2026-05-20T23:22:34+00:00

I just did this question out of KN King’s C Programming: A Modern Aprroach.

  • 0

I just did this question out of KN King’s C Programming: A Modern Aprroach. It’s not homework, I’m just teaching myself out of the book…

The following table shows the daily flights from one city to another:
Departure time – Arrival time
8:00am – 10:16am
9:43am – 11:52am
11:19am – 1:31pm
12:47pm – 3:00pm
2:00pm – 4:08pm
3:45pm – 5:55pm
7:00pm – 9:20pm
9:45pm – 11:58pm

Write a program that asks user to enter a time (expressed in hours and minutes, using the 24 hour clock). The program then displays the departure and arrival times for the flight whose departure time is closest to that entered by the user:

Enter a 24 hour time: 13:15
Closest depature time is 12:47pm., arriving at 3:00pm.

Hint: Convert the input into a time expressed in minutes since midnight, and compare it to the departure times, also expressed in minutes since midnight. For example, 13:15 is 13 x 60 + 15 = 795 minutes since midnight, which is closer to 12:47pm (767 minutes since midnight) than to any of the other departure times.

So far we have only covered basic comparison expressions and the if and switch statement, so my answer has to be based around these obviously and nothing too fancy. My code that I’ve come up with is below, I’m wondering if someone would be willing to take a look at it and see if I’m on the right track, it seems to work but seems to be a lot of code for such a small thing. Maybe it was just to teach us the logic involved. I haven’t pasted the whole code, the rest is just the same thing over and over as it compares the values. I have no programming experience, so please be gentle!

Thanks for your time,
Andrew

#include <stdio.h>

int main (int argc, const char * argv[]) {


// Flight departure times since midnight
// 8am, 9:45am, 11:19am, 12:47pm
// 2pm, 3:45pm, 7pm, 7:45pm
int a = 480, b = 585, c = 679, d = 767,
    e = 840, f = 945, g = 1140, h = 1185;

// Flight arrival times for respective departure times.
int a1 = 616, b1 = 712, c1 = 811, d1 = 900,
    e1 = 968, f1 = 1075, g1 = 1280, h1 = 1438;

int hours, minutes, time, t, u;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
    printf("8:00am");
else
    if (time > a && time <= b) {
                t = time - a; 
                u = b - time;
                if (t < u) {
                    printf("%.2d:%.2d", a / 60, a % 60);
                        if (a / 60 == 0)
                            printf("am");
                        else if (a / 60 < 12)
                            printf("am");
                        else if (a / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
                        if (a1 / 60 == 0)
                            printf("am");
                        else if (a1 / 60 < 12)
                            printf("am");
                        else if (a1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                }
                else {
                        printf("%.2d:%.2d", b / 60, b % 60);
                        if (b / 60 == 0)
                            printf("am");
                        else if (b / 60 < 12)
                            printf("am");
                        else if (b / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", b1 / 60, b1 % 60);
                        if (b1 / 60 == 0)
                            printf("am");
                        else if (b1 / 60 < 12)
                            printf("am");
                        else if (b1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                }
    }

Changes as per advice I’ve been given: (Thanks xamypah and Gabe)
…

int hours, minutes, time, t, u, x, y;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
    printf("8:00am");
else
    if (time > a && time <= b) {
                t = time - a; 
                u = b - time;

                if (t <= u) {
                    x = a;
                    y = a1;
                }
                else {
                    x = b;
                    y = b1;
                }

Then at the end of the program after several of the above:

printf("%.2d:%.2d", x / 60, x % 60);

if (x / 60 < 12)
    printf("am");
else 
    printf("pm");
    printf(", arriving at %d:%.2d", y / 60, y % 60);

if (y / 60 < 12)
    printf("am");
else
    printf("pm");

Actually I had to make some changes to that end print statement, or else it was displaying the times in 24 hour format with am and pm after:

if (x / 60 < 12)
    printf("%d:%.2d am", x / 60, x % 60);
else 
    printf("%d:%.2d pm", (x / 60) - 12, x % 60);

printf(", arriving at ");

if (y / 60 < 12)
    printf("%d:%.2d am", y / 60, y % 60);
else
    printf("%d:%.2d pm", (y / 60) - 12, y % 60);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:22:34+00:00Added an answer on May 20, 2026 at 11:22 pm

    Well, yes you are on the right track, but you also can significantly reduce the size of code.
    Look, the same piece of code, I think, is repeated very often in your program:

                        printf("%.2d:%.2d", a / 60, a % 60);
                            if (a / 60 == 0)
                                printf("am");
                            else if (a / 60 < 12)
                                printf("am");
                            else if (a / 60 == 12)
                                printf("pm");
                            else
                                printf("pm");
                        printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
                            if (a1 / 60 == 0)
                                printf("am");
                            else if (a1 / 60 < 12)
                                printf("am");
                            else if (a1 / 60 == 12)
                                printf("pm");
                            else
                                printf("pm");
    

    It is repeated for a, for b etc…

    And it is usually done at the end of the execution.

    So, if you break your algorithm on two parts: first – finding the right time range, and the second – printing it out, then you get rid off this copy/pasting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently found myself writing this line of code, which i did not
this is a homework which I already did, I just need either confirmation if
This question goes beoynd just programming, but I'd like to get some input on
Okay I just typed this whole question out and then managed to delete it.
I searched and found this question but did not like the answer. Is there
First off, this question is ripped out from this question. I did it because
This seems very foolish mistake, I just did a git stash pop on a
Before asking this i did A LOT of searching on the net. I just
This is just one of those little things I did all the time in
This question may have been asked already - but unfortunately, I could not find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.