
-----------------------------------
Luffy123
Wed May 16, 2012 8:14 am

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  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.

-----------------------------------
Raknarg
Wed May 16, 2012 9:13 am

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 :P

-----------------------------------
QuantumPhysics
Wed May 16, 2012 11:09 am

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

-----------------------------------
Raknarg
Wed May 16, 2012 11:48 am

RE:Help Question
-----------------------------------
I didnt say amount of lines, I said amount of steps, there's a difference.

-----------------------------------
Luffy123
Wed May 16, 2012 7:09 pm

Re: 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 :P

Can you show me how you would do it? I'm a bit confused.

-----------------------------------
Raknarg
Thu May 17, 2012 1:35 pm

RE:Help Question
-----------------------------------
Seeing as this program is so simple, you could simply use nested ifs.


if money > 0 then
     Calculate for a certain amount
     Subtract hours used
     if money > 0 then
          Calculate for a certain amount
          Subtract hours used
          if money > 0 then
               Calculate for a certain amount
               Subtract hours used
          end if
     end if
end if


This would be the simplest way to so it, in my opinion.

-----------------------------------
Dreadnought
Thu May 17, 2012 3:07 pm

Re: Help Question
-----------------------------------
Here's a very simple way :P
put min (1, numOfHours) + min(5, numOfHours) + min (10, numOfHours)
Or if you want something more structured.
put min (min (1, numOfHours) * 3 + max (0, min (4, numOfHours - 1)) * 2 + max (0, numOfHours - 5), 16)
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.

-----------------------------------
Raknarg
Fri May 18, 2012 9:24 am

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 ;) ).
