Thread: Help on C with conversion and adjustment of negatives in time

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Help on C with conversion and adjustment of negatives in time

    Hi all,

    I am using the textbook "Programming, Problem Solving, and Abstraction with C" by Alistair Moffat as a self learning tool and for Chapter 2 Exercise 2.3 (e) having trouble in solving the negative time problem.

    Wondering if there are any insights to a negative time problem I am facing?

    Brief of the problem is:

    1. declared variables for difference of final and start time (final - start)
    2. used scanf function to collect the time in hours, minutes and seconds in 24 hour format.
    3. used the formula as per (1.) and printf function to output the time elapsed in hours, minutes and seconds.

    the code works great but when start time minutes and / or seconds are larger than respective finish time values returns negative.

    obviously gotta convert and adjust the negatives for a "rational" time that is non-negative and adjusting the higher digits of time for the subtraction.

    my code is as follows:

    #include <stdio.h>


    int
    main(int argc, char **argv) {
    int start_hour, start_min, start_sec, finish_hour, finish_min, finish_sec ;
    int elapsed_hour, elapsed_min, elapsed_sec ;

    printf("Please be advised that the time between start and finish is assumed on the same day\n") ;

    printf("Please enter starting time in hours, minutes, seconds in 24 hour format: ") ;
    scanf("%d %d %d", &start_hour, &start_min, &start_sec) ;
    printf("Please enter finish time in hours, minutes, seconds in 24 hour format: ") ;
    scanf("%d %d %d", &finish_hour, &finish_min, &finish_sec) ;

    elapsed_hour = finish_hour-start_hour ;
    elapsed_min = finish_min-start_min ;
    elapsed_sec = finish_sec-start_sec ;

    printf("elapsed time is %d hours, %d minutes, %d seconds", elapsed_hour, elapsed_min, elapsed_sec) ;


    return 0 ;

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You could maybe deal with it by "borrowing" from the next highest "denomination". Something like:
    Code:
    if (finish_sec < start_sec) {
        finish_sec += 60;   // borrow 60 seconds
        finish_min--;       // from the minutes
    }
    if (finish_min < start_min) {
        finish_min += 60;   // borrow 60 minutes
        finish_hour--;      // from the hours
    }
    Alternatively (and perhaps better), you could convert the times to seconds, subtract those, then convert back to hours, minutes and seconds.
    Code:
    start_sec += start_hour * 3600 + start_min * 60;
    finish_sec += finish_hour * 3600 + finish_min * 60;
    
    if (finish_sec < start_sec) {
        printf("The finish time is less than the start time!\n");
    }
    else {
        elapsed_sec = finish_sec - start_sec;
        elapsed_hour = elapsed_sec / 3600;  // integer division "rounds" down
        elapsed_sec %= 3600;                // modulus operator yields remainder
        elapsed_min = elapsed_sec / 60;
        elapsed_sec %= 60;
        printf("%02d:%02d:%02d\n", elapsed_hour, elapsed_min, elapsed_sec);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    Thanks John for the detailed insight.

    I am a newb self-teaching student of programming and probably until I get to the section on if statements won't probably get it but wondering if I could trouble you for a bit of an explanation on why on the "else" part of the code what actually happens to the elapsed_sec %= 3600 being repeated on elapsed %= 60? Does that not overwrite the elapsed_sec to the latter?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding max and min values including negatives
    By 3DT in forum C Programming
    Replies: 11
    Last Post: 02-26-2014, 06:28 AM
  2. Time conversion
    By JonaRab in forum C Programming
    Replies: 2
    Last Post: 01-08-2011, 03:48 AM
  3. Checking for negatives in an unsigned variable
    By samus250 in forum C Programming
    Replies: 9
    Last Post: 04-21-2008, 01:21 AM
  4. Mod with negatives
    By wuzzo87 in forum C Programming
    Replies: 9
    Last Post: 04-11-2008, 10:54 PM
  5. Time....conversion (911)
    By Semma in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 08:36 AM

Tags for this Thread