Computer Science Canada Help Question |
Author: | Luffy123 [ Wed May 16, 2012 8:14 am ] |
Post subject: | Help Question |
Read over the following problem and write a program which accepts the number of hours that a car has parked in a parking lot and then calculates the charge based on the following schedule: ? $3 per hour for the first hour ? $2 per hour for the next 4 hours ? $1 per hour for the remaining time parked ? The MAXIMUM charge is $16 Save your program as L3A1Q3. The way I'm doing works. But doesn't seem right. var numOfHours, cost : int put "Enter the number of hours the car has parked." get numOfHours if numOfHours <= 1 then cost := numOfHours * 3 elsif numOfHours <= 5 then cost := numOfHours * 2 + 1 elsif numOfHours > 5 then cost := numOfHours * 1 + 6 end if if cost > 16 then cost := 16 end if put "Cost: ", cost I know there is a better way of doing it. But I can't figure it out. |
Author: | Raknarg [ Wed May 16, 2012 9:13 am ] |
Post subject: | RE:Help Question |
Not quite. How you should be doing it is cutting the cost into slices, rather than doing it all in one step. So basically what yuou need to write is a program that has a maximum of four steps, and doesnt continue when one of those step is not met. Ex. you stay 4 hours. The first hour costs on dollar that we put in a pot. Then we subtract an hour from it. We have 3 hours left. We add 2 dollars per hour into the old pot, then subtract 4 hours (which was the condition) We have -1 hours left. Therefore, the next condition is impossible, and we can stop there. From this, we concluded that 4 hours costs 7 dollars. You can do it other ways, I just thought that sounded simple. I may be wrong ![]() |
Author: | QuantumPhysics [ Wed May 16, 2012 11:09 am ] |
Post subject: | RE:Help Question |
well if your going for an amount of lines you can put it all on the same line by just repeated all the lines you made and put a semicolon former to C++ after each consecutive line. It will look as if you made it on one line. But it will not be organized |
Author: | Raknarg [ Wed May 16, 2012 11:48 am ] |
Post subject: | RE:Help Question |
I didnt say amount of lines, I said amount of steps, there's a difference. |
Author: | Luffy123 [ Wed May 16, 2012 7:09 pm ] |
Post subject: | Re: RE:Help Question |
Raknarg @ Wed May 16, 2012 9:13 am wrote: Not quite. How you should be doing it is cutting the cost into slices, rather than doing it all in one step. So basically what yuou need to write is a program that has a maximum of four steps, and doesnt continue when one of those step is not met.
Ex. you stay 4 hours. The first hour costs on dollar that we put in a pot. Then we subtract an hour from it. We have 3 hours left. We add 2 dollars per hour into the old pot, then subtract 4 hours (which was the condition) We have -1 hours left. Therefore, the next condition is impossible, and we can stop there. From this, we concluded that 4 hours costs 7 dollars. You can do it other ways, I just thought that sounded simple. I may be wrong ![]() Can you show me how you would do it? I'm a bit confused. |
Author: | Raknarg [ Thu May 17, 2012 1:35 pm ] | ||
Post subject: | RE:Help Question | ||
Seeing as this program is so simple, you could simply use nested ifs.
This would be the simplest way to so it, in my opinion. |
Author: | Dreadnought [ Thu May 17, 2012 3:07 pm ] | ||||
Post subject: | Re: Help Question | ||||
Here's a very simple way ![]()
Or if you want something more structured.
If you read the second one carefully, you'll see that I charge $3 for the first hour, $2 for the next four and $1 per hour afterwards, with a maximum of 16. Btw, this is kinda bad practice, something like what Raknarg posted is much better. |
Author: | Raknarg [ Fri May 18, 2012 9:24 am ] |
Post subject: | RE:Help Question |
yeah, I said you can do it in one line or not. The thing is that this program is so simple, I think it might be better just to do it the lazy way (aka yours ![]() |