Download presentation
Presentation is loading. Please wait.
Published byTheresa Conley Modified over 9 years ago
1
Program #2 Algorithm for Parking at PSU
2
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 permit Park on the street (at a meter) Park in a pay lot (like Smart Park)
3
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 $279 Meters on the street are $1.25 per hour Smart parking depends on the number of hours there. For 4 hours or less it is $1.25 per hour For more than 4 hours it is $3 per hour Most lots have a maximum charge (e.g., $11)
4
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!
5
Data We will Need from User…. How many days they park downtown How many hours (during the day) per day on average Price of the PSU term long parking pass Price of the meters per hour Price of short term parking at a Parking lot Price of long term parking at a Parking lot Maximum price per day at a Parking lot
6
Some of the Variables…. This means we will need variables for at least: Number of days parking downtown Average number of hours per day Price of the PSU term long parking pass Price of the meters per hour Price of short term parking at a Parking lot Price of long term parking at a Parking lot Maximum price per day at a Parking lot
7
Before we start the algorithm…. Before we start the algorithm, I have some hints and suggestions Start small. Do one piece and once it works then build upon it So, 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.
8
Major Tasks of the Algorithm The Major Tasks of the Algorithm are: Welcome the User Get the Data from the User Prompt, Input, Echo the data for each item Calculate the Monthly rate Display the Results Display a Termination Message
9
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 PSU Prompt, 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)
10
Just for PSU Parking Calculate the Monthly rate A 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 Message Plan what you want to say here
11
Add in the Price for Meters! So, now let’s expand the algorithm to include the price of meters Welcome 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)
12
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)
13
Parking at a Meter Calculate the Monthly rate for a meter Multiply the cost of a meter by the number of hours you park per day Multiply this by the number of days per week Multiply 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 Message Plan what you want to say here
14
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.cpp Now, you can run the program (once you get out all of the errors by typing.\a.out
15
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 day Prompt, 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)
16
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
17
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.cpp Get the typos out by re-editing…and run it!.\a.out
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.