Simple algorithm question

Discussion in 'C++' started by newbie, Apr 17, 2007.

  1. newbie

    newbie Guest

    This is an embarassingly easy algorithm, but I seem to have got a mental
    block -

    Here is a code snippet:

    void OffsetToParts(const short midnightOffset, short *hours, short
    *minutes, short *seconds)
    {
    hours = midnightOffset / 3600;
    minutes = (midnightOffset / 60) % 60;
    seconds = midnightOffset % 60;
    }

    Now I want to write a function like this :

    short PartsToOffset(const short hours, const short minutes, const short
    seconds)
    {
    // ...
    }

    Any helpers out there ?
    newbie, Apr 17, 2007
    #1
    1. Advertising

  2. newbie

    Fei Liu Guest

    newbie wrote:
    > This is an embarassingly easy algorithm, but I seem to have got a mental
    > block -
    >
    > Here is a code snippet:
    >
    > void OffsetToParts(const short midnightOffset, short *hours, short
    > *minutes, short *seconds)
    > {
    > hours = midnightOffset / 3600;
    > minutes = (midnightOffset / 60) % 60;
    > seconds = midnightOffset % 60;
    > }
    >
    > Now I want to write a function like this :
    >
    > short PartsToOffset(const short hours, const short minutes, const short
    > seconds)
    > {
    > // ...
    > }
    >
    > Any helpers out there ?
    >
    >

    This is probably off topic, but you are looking for something like this:
    hour * 3600 + minute * 60 + second

    Now just translate that into C++ and finish the function PartsToOffset.

    F
    Fei Liu, Apr 17, 2007
    #2
    1. Advertising

  3. newbie

    anon Guest

    newbie wrote:
    > This is an embarassingly easy algorithm, but I seem to have got a mental
    > block -
    >
    > Here is a code snippet:
    >
    > void OffsetToParts(const short midnightOffset, short *hours, short
    > *minutes, short *seconds)
    > {
    > hours = midnightOffset / 3600;
    > minutes = (midnightOffset / 60) % 60;
    > seconds = midnightOffset % 60;
    > }
    > Any helpers out there ?
    >


    I do not know your requirements, but in OffsetToParts function, you are
    changing the address of hours, minutes and seconds
    anon, Apr 17, 2007
    #3
  4. anon wrote:
    > newbie wrote:
    >> This is an embarassingly easy algorithm, but I seem to have got a
    >> mental block -
    >>
    >> Here is a code snippet:
    >>
    >> void OffsetToParts(const short midnightOffset, short *hours, short
    >> *minutes, short *seconds)
    >> {
    >> hours = midnightOffset / 3600;
    >> minutes = (midnightOffset / 60) % 60;
    >> seconds = midnightOffset % 60;
    >> }
    >> Any helpers out there ?
    >>

    >
    > I do not know your requirements, but in OffsetToParts function, you
    > are changing the address of hours, minutes and seconds


    In C++ I'd probably pass each short by reference, not a pointer to it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 17, 2007
    #4
  5. newbie

    red floyd Guest

    newbie wrote:
    > This is an embarassingly easy algorithm, but I seem to have got a mental
    > block -
    >
    > Here is a code snippet:
    >
    > void OffsetToParts(const short midnightOffset, short *hours, short
    > *minutes, short *seconds)
    > {
    > hours = midnightOffset / 3600;
    > minutes = (midnightOffset / 60) % 60;
    > seconds = midnightOffset % 60;
    > }
    >
    > Now I want to write a function like this :
    >
    > short PartsToOffset(const short hours, const short minutes, const short
    > seconds)
    > {
    > // ...
    > }
    >
    > Any helpers out there ?



    You are aware that there are 86400 seconds in a day, and that on most
    platforms, that won't fit into a short, right?
    red floyd, Apr 17, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    0
    Views:
    773
    Ahmed Moustafa
    Nov 15, 2003
  2. savesdeday

    Simple Algorithm

    savesdeday, Mar 1, 2004, in forum: C++
    Replies:
    17
    Views:
    1,553
    Karl Heinz Buchegger
    Mar 5, 2004
  3. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,493
    Mike Treseler
    Jun 23, 2006
  4. someone else

    simple algorithm for finding primes

    someone else, Dec 8, 2003, in forum: C Programming
    Replies:
    32
    Views:
    1,402
    Chris Torek
    Dec 12, 2003
  5. Peter Brooks
    Replies:
    17
    Views:
    152
    Robert Kern
    Jun 12, 2013
Loading...

Share This Page