# Simple algorithm question

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

1. ### newbieGuest

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

2. ### Fei LiuGuest

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

3. ### anonGuest

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
4. ### Victor BazarovGuest

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
5. ### red floydGuest

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