Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help Question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Luffy123




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: 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 Razz
QuantumPhysics




PostPosted: 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
Raknarg




PostPosted: 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.
Luffy123




PostPosted: 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 Razz


Can you show me how you would do it? I'm a bit confused.
Raknarg




PostPosted: 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.

Turing:

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




PostPosted: Thu May 17, 2012 3:07 pm   Post subject: Re: Help Question

Here's a very simple way Razz
Turing:
put min (1, numOfHours) + min(5, numOfHours) + min (10, numOfHours)

Or if you want something more structured.
Turing:
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




PostPosted: 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 Wink ).
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: