Clockwork puzzle

This is a discussion on Clockwork puzzle within the C Programming forums, part of the General Programming Boards category; Okay, I'm trying to create calculator that counts times like: 18:00 - 17:00 = 1 hour I have most of ...

  1. #1
    Beginner
    Guest

    Question Clockwork puzzle

    Okay, I'm trying to create calculator that counts times like:
    18:00 - 17:00 = 1 hour

    I have most of the code ready, but I'm getting wrong answers, there must be error somewhere, but I can't find it... here's the code:

    #include <stdio.h>
    int main ( )
    {
    int hours;
    int min;
    int hours2;
    int min2;
    int total;
    int minutes, time_h, time_min;
    printf("\Give first time in form h min (example 12 34):");
    scanf("%d %d", &hours, &min);
    printf("\Give second time in form h min (example 12 34):");
    scanf("%d %d", &hours2, &min2);
    if ((hours >= 0)&&(hours <=23)&&(min >= 0)&&(min<=59))
    {
    total = hours / 60 + min - hours2 / 60 + min2;
    printf("%d", total);
    printf("\n");
    minutes = 1440 - (min + 60*hours);
    time_h = minutes / 60;
    time_min = minutes % 60;
    printf("The time was %d minutes", total);
    }
    else
    printf("You gave incorrect clock time!");
    return 0;
    }



    Don't mind about the time_h and time_min, those don't have anything to do with the problem I'm trying to solve now...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > 18:00 - 17:00 = 1 hour
    Convert both times into minutes

    So you have
    diff = (18*60+0) - (17*60+0)

    Which gives you the time difference in minutes

    To turn this back into hours and minutes, its just
    diff_hr = diff / 60;
    diff_mn = diff % 60;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Beginner
    Guest

    Not quite sure if I got that...

    My code now:

    #include <stdio.h>
    int main ( )
    {
    int hours;
    int min;
    int hours2;
    int min2;
    int total;
    int minutes, time_h, time_min;
    int diff;
    int diff_hr;
    int diff_mn;
    printf("\Give first time in form h min (example 12 34):");
    scanf("%d %d", &hours, &min);
    printf("\Give second time in form h min (example 12 34):");
    scanf("%d %d", &hours2, &min2);
    if ((hours >= 0)&&(hours <=23)&&(min >= 0)&&(min<=59))
    {
    total = hours / 60 + min - hours2 / 60 + min2;
    printf("\n");
    minutes = 1440 - (min + 60*hours);
    diff = (hours*min) - (hours2*min2);
    diff_hr = diff / 60;
    diff_mn = diff % 60;
    time_h = minutes / 60;
    time_min = minutes % 60;
    printf("test1 %d minutes test2 %d minutes %d %d", total, diff, diff_hr, diff_mn);
    }
    else
    printf("You gave incorrect clock time!");
    return 0;
    }


    Didn't help... but I'm not sure if you meant this...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    FOCUS!!!!!
    Did you see any hours * minutes ?
    Does hours * minutes make any sense?

    Code:
    #include <stdio.h>
    
    int main ( ) {
        int hours;
        int min;
        int hours2;
        int min2;
        int diff;
        int diff_hr;
        int diff_mn;
    
        printf("Give start time in form h min (example 12:34):");
        scanf("%d:%d", &hours, &min);
        printf("Give end   time in form h min (example 12:34):");
        scanf("%d:%d", &hours2, &min2);
    
        diff = ( hours2 * 60 + min2 ) - ( hours * 60 + min );
        diff_hr = diff / 60;
        diff_mn = diff % 60;
        printf("Difference is %d:%d\n", diff_hr, diff_mn);
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-06-2008, 06:26 PM
  2. Clockwork development build
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 01-20-2007, 08:11 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2006, 12:08 AM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 10:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21