Page 1 of 1

Unclear algorithm definition

Posted: Sun Apr 12, 2009 8:18 am
by fsx
Hello everyone,

I'm new to this forum pleased to meet you all.

I was trying to solve a definition of an algorithm but it seems I'm pretty much stuck, due to what it seems an unclear definition in the book (and there's no solution either).

Here we go:

TELEPHONE RATES
The rate for use of a telephone depends on the time of call, which is measured as MPMs or minutes past midnight (ranging from 0 to 60 x 24).
The "day rate" from 6am to 6pm is given in dollars per inute, and the "night rate" as a proportion of this day rate.
If the rate is determined at the beginning of the call and remains fixed at that value, create an algorithm that computes the total cost for any call given the start time and the terminating time, both in MPMs.
If the rate can change during a call (when it lasts past the 6 o'clock times) then create the new algorithm to compute the cost.


Now, my problem is that I don't understand how can I extract a "proportion" from all this, because to me seems NOT DEFINED! Ok I could create the algorithm as black boxes with one labeled simply "Proportion", without worrying about the exact computation to be done, but... what do you think?

Thank in advance and happy easter! =)

FSX

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 8:40 am
by Nitrodist
You can work with a dummy value for the day rate and night rate. The reason why they don't give you the actual rates is so that you can write a method on its own where the method accepts differing values. This gives the method portability to other programs.

If you had just made it within your main method, then you would have to retype some of it instead of just copying it over.

Reading the question is quite easy as it quantifies what you need in your problem.

TELEPHONE RATES
The rate for use of a telephone depends on the time of call, which is measured as MPMs or minutes past midnight (ranging from 0 to 60 x 24).


So the format of the minutes will range from 0 to 1440. There you should put an int in for your MPM readings.

The "day rate" from 6am to 6pm is given in dollars per minute, and the "night rate" as a proportion of this day rate.


You will likely need to keep track of two numbers: the minutes in the day time and the minutes in the night time.

If the rate is determined at the beginning of the call and remains fixed at that value, create an algorithm that computes the total cost for any call given the start time and the terminating time, both in MPMs.


Three cases for which to base your method on. If the MPM falls between day time, do the day time case. Vice versa for night time. The third case is for when it falls between night time and day time.

If the rate can change during a call (when it lasts past the 6 o'clock times) then create the new algorithm to compute the cost.

See above.

I'm assuming the problem is going to give you two times for the MPM; the time when it started and when it ended. So what you need to put into the method will be a few things;

An int for the beginning of the call in MPM.
An int for the ending of the call in MPM.
A double for the day time rates.
A double for the proportion of the day time rates to the night time rates. (1 to [number goes here] so that your double will likely be .7 or something, thus easily allowing you to multiply)


An example of the code;

public static double computePhoneCall (int beginMPM, int endMPM, double dayRate, double proportion) {

double nightRate = dayRate * proportion;
double cost;

....
....
....

return cost;
}

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 8:57 am
by notfred
Forget the doubles, floating point should be avoided where possible - you frequently don't have it in embedded systems and even when you do have it integer math is usually way faster. Make the rate in cents and the day rate automatically becomes a uint. Make the proportion a divisor and you should be able to get away with another uint. Also note that the other ints should be unsigned!

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:14 am
by fsx
honestly I still don't understand the algorithm. why the minutes past midnight are defined from 0 to 60? that's only one hour possible after midnight. really this definition is impossible to understand for me...

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:21 am
by Nitrodist
0 - 60 x 24. Times 24. Multiplied by 24. 24 hours in a day.

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:46 am
by JustAnEngineer
There are still 1440 minutes in a day. MPM will therefore be in the range of 0-1439.

If you want to really generalize, set the 6:00 AM to 6:00 PM peak rate start and stop times as defined constants rather than sticking the numbers 360 and 1080 in your calculation code.

Once you've written your code, check how it handles unusual input conditions, such as a call that continues past midnight or last longer than 720 minutes.

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:49 am
by fsx
ok 24 hours in a day BUT how do I determine how many hours past midnight? for example, the phonecall starts at 17:45 (5.45 PM) so I have to do 45 * 17 to find the minutes past midnight? thanks again

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:50 am
by JustAnEngineer
Create an algorithm that computes the total cost for any call given the start time and the terminating time, both in MPMs.
The problem statement clearly says that you will be given the start and finish times in MPM. Therefore, you start with two numbers between 0 and 1439, and you work some magic to determine what the cost of the call will be. The code doesn't care what the hours are. It only knows about MPM.

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:52 am
by Nitrodist
Why is it 1039?

Re: Unclear algorithm definition

Posted: Sun Apr 12, 2009 9:54 am
by JustAnEngineer
Because I'm typing poorly with one hand while eating breakfast with the other. Doh! :oops: