Logic of this question (Homework)

Can someone tell me the basic logic of this program? I don't want the code, just this that what exactly is it I am supposed to do here? I can't seem to understand the question.
(This is basically a pic. I don't know how to post a pic here so I'm trying to paste it in words in best format possible)

Assign a value to double variable cost depending on the value of integer variable distance as follows:

Distance Cost
------------ --------
0 through 100 5.00
More than 100 but not more than 500 8.00
More than 500 but less than 1,000 10.00
1,000 or more 12.00


Now, what I get from it is I am supposed to assign one integer for distance and input it from the user and then I have to output the cost of that distance (like if he inputs 56, program should output Cost = 5.00)
But then what is the point of assigning a double variable? Since the cost is also an integer value and not a decimal value.. Or is it only because I must also show two "Zeroes" with 5? (which I can easily do by using setpricision(2) function)

My concern is this that I don't think the program would be so easy... And hence, I am confused whether I am understanding it the right way or not.
Would be really glad for any help. Thank you!
(Sorry for my English, it's not my first language)
 
  No code
Btw try picture hosting sites like imgur.

You know how electricity bills are calculated right? It's just like that.

For units up to 100 cost $5.00, for 100 to 500 (i,e 400 units) cost $8
So for example for 101 units you would have to pay 100x$5+1x$8
Last edited on
@Nwb Ok, thank you... So, still, basically the result will always be an integer, right?
I just need to declare a double and set the precision to two and add the number up?
So, for example, for 542, it should give "Cost = $4120.00"
Yes you're right 542 would cost $4120.00 according to the given costs

In this case we're talking about distance and not units. I don't think the writer would explicitly mention two decimal places for the costs if the output could only be integers (in this case the output can only be a floating point if the input is in floating point because the costs are in whole numbers, hence the input must be in floating point).

So both input (of distance) must be in double and output must be in double.

Remember that double can support integer inputs just as well as int for small numbers so it won't make a difference if the program were to take integers either way. ;)

You can set the precision if you want (that only changes the display of the output and not the output itself so it's fine).

Good luck Hassan!
Last edited on
Yup, that's what I thought also.. But that integer word in the program confused me a little lol..
Anyway, I still took a double for distance as well, just in case if he wants to add a decimal value... It only improves the program hehe..

And thank you very much! ^_^ Program is working fine... Though there seems to be another problem with high number calculations :P I think I will post another question if I can't figure it out :/
You can use 'long double' ;) but I don't think you will require numbers large than that ;P
Not the problem of decimals. It's more of a problem of not giving accurate answer :/
I will ask it in a separate post.. Couldn't figure out myself :/
Long double only starts to lose precision after 18 digits and that's a lot of digits mind you. But if you still want more precision then you should get some arbitrary precision arithmetic library for floating points. ;)

Topic archived. No new replies allowed.