473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple algorithm question

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 ?
Apr 17 '07 #1
4 1573
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
Apr 17 '07 #2
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

Apr 17 '07 #3
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
Apr 17 '07 #4
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?
Apr 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob | last post by:
He , I need to generate some random numbers, right now I am using: Random pk = new Random(); int a = pk.nextInt(540); and wondering if there is any other methods in java to generate random...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
55
by: copx | last post by:
Can anyone point me to a simple, fast RRNG function to generate random ints within a specified range? It is important that each value within the range has the same probability (uniform...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.