program #2 algorithm for parking at psu. understanding the assignment you will be writing a program...
TRANSCRIPT
Program #2
Algorithm for Parking at PSU
Understanding the Assignment
You will be writing a program to find out how much someone at PSU might be spending on Parking
Help the user figure out if they should…Buy a parking permitPark on the street (at a meter)Park in a pay lot (like Smart Park)
Cost of Parking….
I looked up parking rates for Winter 2009 and this is what I found…A term long pass at PSU (11 weeks) is $279Meters on the street are $1.25 per hourSmart parking depends on the number of
hours there.For 4 hours or less it is $1.25 per hourFor more than 4 hours it is $3 per hourMost lots have a maximum charge (e.g., $11)
Cost of Parking….
So, although we know what these rates are…right now…They are subject to change If we “hard code” these numbers into our
program, our program would never work for another term or another University or situation
So, we will use variables instead!
Data We will Need from User….
How many days they park downtownHow many hours (during the day) per day
on averagePrice of the PSU term long parking passPrice of the meters per hourPrice of short term parking at a Parking lotPrice of long term parking at a Parking lotMaximum price per day at a Parking lot
Some of the Variables….
This means we will need variables for at least:Number of days parking downtownAverage number of hours per dayPrice of the PSU term long parking passPrice of the meters per hourPrice of short term parking at a Parking lotPrice of long term parking at a Parking lotMaximum price per day at a Parking lot
Before we start the algorithm….
Before we start the algorithm, I have some hints and suggestionsStart small. Do one piece and once it works
then build upon itSo, first just input the price for parking at the
PSU lots (e.g., $279 for 12 weeks) and compute the monthly rate. Display this
Once that works, then move on to calculating the price at a meter! And, so on.
Major Tasks of the Algorithm
The Major Tasks of the Algorithm are:Welcome the UserGet the Data from the User
Prompt, Input, Echo the data for each itemCalculate the Monthly rateDisplay the ResultsDisplay a Termination Message
Just for PSU Parking
So, let’s look at a detailed algorithm for just calculating the price for parking at PSU Welcome the User (plan what you want to say)
Get the Price of parking for a term at PSUPrompt, Read, and Echo each
Prompt: Please enter the cost of buying a permit at PSU Read in the cost (using cin) Echo the cost (using cout)
Just for PSU Parking
Calculate the Monthly rateA term is 11 weeks long (for example)Divide the cost by 11.0 (using floats!)Multiply the answer by 4 (number of weeks in a
month) Cost per month = (Term Rate Divided by 11.0 ) times 4
Display the Results (using cout) “ Your cost at PSU parking lots will be $ “ …
Display a Termination MessagePlan what you want to say here
Add in the Price for Meters!
So, now let’s expand the algorithm to include the price of metersWelcome the User (plan what you want to say)
Get the Number of Days, Average Number of Hours, Price of parking at a meter
Prompt, Read, and Echo each Prompt: Please enter the number of days that you park
per week Read in the number of days (using cin) Echo the number of days (using cout)
Parking at a Meter
Prompt: Please enter the average number of hours you park Read in the number of hours (using cin) Echo the number of hours (using cout)
Prompt: Please enter the cost of parking at a meter Read in the cost (using cin) Echo the cost (using cout)
Parking at a Meter
Calculate the Monthly rate for a meterMultiply the cost of a meter by the number of hours
you park per day Multiply this by the number of days per weekMultiply this by 4
Cost per month = Meter Cost x hours per day x days per week x 4
Display the Results (using cout) “ Your approximate cost of street parking will be $ “
Display a Termination MessagePlan what you want to say here
Just for PSU Parking
Now, add this, piece by piece into your C++ program (maybe name it something else)
Enter it with an editor (like pico) in your cs161 directory
Save it as prog2b.cpp When you are back at the Unix prompt type:
g++ prog2b.cppNow, you can run the program (once you get
out all of the errors by typing .\a.out
Using a Parking lot (Smart Park)
So, if you are going for the extra credit, let’s keep going and add in the ability to park at a parking lot (other than PSU)
The new data we need to read in is:Get the price of short term parking, price of long
term parking, maximum price per dayPrompt, Read, and Echo each
Prompt: Please enter the short term, long term and maximum prices
Read in all three (e.g., using 3 cin statements) Echo the all three (using cout)
Parking at Smart Park
Calculate the Monthly rate for Smart Park If the number of hours per day is less than or equal
to 4, Cost = number of hours * number of days * short term cost
Otherwise if the number of hours times the long term rate is larger than the maximum parking amount,
Cost = number of days * maximum cost per day
Otherwise, Cost = number of hours * number of days * long term cost
Believe it or not, we are done!
Now, add this, piece by piece into your C++ program (maybe name it something else)
Enter it with an editor (like pico) in your cs161 directory
Save it as prog2c.cpp When you are back at the Unix prompt type:
g++ prog2c.cppGet the typos out by re-editing…and run it!
.\a.out