Thursday, August 21, 2014

New Program to Oggle

I finally hammered out the computational error in the program project I've been working on this week the past day or two. As with most of the programs out of this book, it is riddled with specific tools the author wants to be used at this point, and arbitrary numbers (which, are hopefully less-arbitrary with the comments I've written). Have a gander:

/* Programming Project #8 Chapter 5                                *
* Write a program that asks the 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.          */


#include <stdio.h>

int main (void)    {

    int hours_entry, minutes_entry, min_since_midn;
                          /* minutes since midnight */

    printf("Enter a 24-hour time: ");
    scanf(
"%d:%d", &hours_entry, &minutes_entry);
   
    if (hours_entry > 24)    {    
         /* Error message for invalid hour value */

        printf("Invalid hour value; please try again.\n");
        return 0;
        }
       
    if (minutes_entry > 59)    {   
         /*Error message for invalid minute value */

        printf("Invalid minute value; please try again.\n");
        return 0;
        }
   
    min_since_midn = hours_entry * 60 + minutes_entry;
   
    if (min_since_midn <= 532)   
           /* 8:00am is the first departure, converted to
            * minutes equals 480 minutes since midnight, plus
            * the equivalent of half of the amount of minutes
            * between 8:00am and 9:43am */

        printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.\n");
    else if (min_since_midn <= 631)
            /* 9:43am is the second departure, same as above */
        printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.\n");
    else if (min_since_midn <= 723)
            /* 11:19am is the third departure */
        printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.\n");
    else if (min_since_midn <= 804)   
            /* 12:47pm is the fourth departure */

        printf("Closest departure time is 12:47 p.m., arriving at 3:00 p.m.\n");
    else if (min_since_midn <= 893)   
            /* 2:00pm is the fifth departure */

        printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.\n");
    else if (min_since_midn <= 938)   
            /* 3:45pm is the sixth departure */
        printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.\n");
    else if (min_since_midn <= 1223)   
            /* 7:00pm is the seventh departure */
        printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.\n");
    else if (min_since_midn <= 1439)   
            /* 9:45pm is the last departure, so all times after *

             *
9:45 will display this time */
        printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.\n");
    else
        printf("Invalid entry; please try again.\n");
       
    return 0;
}
Criticisms and advice are both welcome -- just know that I only really have int/float types and if/switch statements with a smattering of logical operators and relational operators in my arsenal at this point in time. Next chapter will be on Loops, and a few chapters later, I'll finally get into Arrays! (I also apologize for the atrocious line-break issue. I did my best to make it pretty and easy to read!)

No comments:

Post a Comment