Algo for create hours and minutes with given seconds

This is a discussion on Algo for create hours and minutes with given seconds within the C Programming forums, part of the General Programming Boards category; Hi...this (semplified) code wait till I press a key, then it looks for the seconds elapsed. But I want to ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Algo for create hours and minutes with given seconds

    Hi...this (semplified) code wait till I press a key, then it looks for the seconds elapsed.

    But I want to divide them into hours/minute/seconds and not only seconds.. the algo I've wrote it's wrong (but it seems ok to me )

    ... can u help me ?

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    
    {
      time_t start,end, timer;
      char Input [256];
      double dif;
      int ore;  //hours
      int minuti; //minutes
      int secondi;  //seconds
    
      timer=time(NULL);
      time (&start);
      printf("Workunit Started   --> %s",asctime(localtime(&timer)));
      gets (Input);
      time (&end);
      dif = difftime (end,start);
      timer=time(NULL);
      printf("Workunit Completed --> %s\n",asctime(localtime(&timer)));
      
      // that's wrong !!! 
      ore = dif/216000;
      minuti = (dif-(ore*216000))/3600;
      secondi = dif-((ore*216000)-(minuti*3600)); 
      
      printf ("Time elapsed : %d:%d:%d \n", ore,minuti,secondi );
      
      system("PAUSE");
     
      return 0;
    }
    thanx for your attention
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If your algorithm doesn't work, try it using a pen and paper.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Doing an average system may help.

    Especially for the minutes etc... It's not that hard of an algorithm, it just takes a while to write and understand.

    Though, jotting it down may help [which I haven't myself] I like playing around with code until I understand how it works and how I did it. I can always give some recommendations of how this code works if you like.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is the purpose of timer in your code? It doesn't do anything, and you never actually use it. Why not just:
    Code:
            start = time( NULL );
            /* do stuff */
            end = time( NULL );
            difference = difftime( end, start );
            /* calculate */
            /* display */
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    ok i'm trying to figure out why it doesnt work

    ps: for Quzah... I need timer in the code I've cut (I preferred to not post a very long code)
    This forum is the best one I've ever seen. Great ppl, great coders

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    somenumber = passednumberofseconds
    somenumber % 60 = the "left over" seconds, this should be stored in a seconds counter
    somenumber / 60 = number of minutes
    number of minutes % 60 = "left over minutes", store this in some minutes counter
    number of minutes / 60 = hours
    This is like the "make correct change" exercise people commonly do. You are in effect "making correct change" from a given number of seconds (cents).

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx to all

    anyway I got it, it was a sign mistake, a - for a +

    this works

    Code:
      ore = dif/3600;
      minuti = (dif-(ore*3600))/60;
      secondi = dif-((ore*3600)+(minuti*60));
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  2. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 05:46 PM
  3. Difference b/ween hours into minutes??
    By CodeFuse in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2003, 01:01 AM
  4. switch hours and minutes
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-08-2002, 11:30 PM
  5. Converting MInutes to Hours and MInutes
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-30-2001, 08:07 PM

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