Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
fsx
Gerbil In Training
Topic Author
Posts: 3
Joined: Sat Apr 11, 2009 12:19 pm

Unclear algorithm definition

Sun Apr 12, 2009 8:18 am

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
 
Nitrodist
Grand Gerbil Poohbah
Posts: 3281
Joined: Wed Jul 19, 2006 1:51 am
Location: Minnesota

Re: Unclear algorithm definition

Sun Apr 12, 2009 8:40 am

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;
}
Image
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Unclear algorithm definition

Sun Apr 12, 2009 8:57 am

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!
 
fsx
Gerbil In Training
Topic Author
Posts: 3
Joined: Sat Apr 11, 2009 12:19 pm

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:14 am

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...
 
Nitrodist
Grand Gerbil Poohbah
Posts: 3281
Joined: Wed Jul 19, 2006 1:51 am
Location: Minnesota

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:21 am

0 - 60 x 24. Times 24. Multiplied by 24. 24 hours in a day.
Image
 
JustAnEngineer
Gerbil God
Posts: 19673
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:46 am

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.
Last edited by JustAnEngineer on Sun Apr 12, 2009 9:54 am, edited 2 times in total.
 
fsx
Gerbil In Training
Topic Author
Posts: 3
Joined: Sat Apr 11, 2009 12:19 pm

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:49 am

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
 
JustAnEngineer
Gerbil God
Posts: 19673
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:50 am

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.
Last edited by JustAnEngineer on Sun Apr 12, 2009 9:53 am, edited 1 time in total.
 
Nitrodist
Grand Gerbil Poohbah
Posts: 3281
Joined: Wed Jul 19, 2006 1:51 am
Location: Minnesota

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:52 am

Why is it 1039?
Image
 
JustAnEngineer
Gerbil God
Posts: 19673
Joined: Sat Jan 26, 2002 7:00 pm
Location: The Heart of Dixie

Re: Unclear algorithm definition

Sun Apr 12, 2009 9:54 am

Because I'm typing poorly with one hand while eating breakfast with the other. Doh! :oops:

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On