Simple algorithm question

N

newbie

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 ?
 
F

Fei Liu

newbie said:
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
 
A

anon

newbie said:
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
 
V

Victor Bazarov

anon said:
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
 
R

red floyd

newbie said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top