Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple algorithm question

Reply
Thread Tools

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 ?


 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Python Based Genetic Algorithm Framework Vishal Patil Python 0 11-08-2006 04:25 AM
Filtered Back Projection Algorithm (FBP Algorithm) Bapaiah Katepalli VHDL 1 06-23-2006 04:50 PM
Simple Algorithm savesdeday C++ 17 03-05-2004 03:48 PM
simple algorithm for finding primes someone else C Programming 32 12-12-2003 08:07 PM
Key generation algorithm and Cipher algorithm Ahmed Moustafa Java 0 11-15-2003 06:35 AM



Advertisments