Velocity Reviews > C++ > Simple algorithm question

# Simple algorithm question

newbie
Guest
Posts: n/a

 04-17-2007
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 ?

Fei Liu
Guest
Posts: n/a

 04-17-2007
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

anon
Guest
Posts: n/a

 04-17-2007
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

Victor Bazarov
Guest
Posts: n/a

 04-17-2007
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
--

red floyd
Guest
Posts: n/a

 04-17-2007
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?