MATLAB Answers

What am I missing?

3 views (last 30 days)
I was asked to create a script:
Assume that fees are established at an airport parking lot as follows: Fee for the first hour of parking is 25 cents. An additional 25 cents is due if a car is parked in the lot for up to 3 hours. A total of 75 cents is due for any car parked for more than 3 hours, up to 6 hours. One dollar is due if a car remains for more than six hours but not more than 24 hours. One dollar is due for each day or portion of a day thereafter, to a maximum of 7 days. No car may be parked in the lot for more than 7 days. Create a MATLAB script to read the car number (a five digit number) and a total parking time (in hours and minutes), then display the car number and the corresponding fee. The script should print the message 'illegal parking' if the parking time of the car doesnt correspond to any of the categories. The script should end if the input from the user is 99999 for the car number, the function should take, as parameters, the total parking time, perform the calculation of the fee, and return it. The main MATLAB script deals with the user input, calls the function, and displays the corresponding output. The output should look like:
10352 0.50
27665 0.25
32009 illegal parking
13422 2.00
14474 1.00
I have managed to pull this together with my limited knowledge of MATLAB:
My Function:
function [t] = ParkingFees(~)
for t = 1:168;
if t == 1
fee = 0.25;
elseif (1 < t) && (t < 3)
fee = 0.50;
elseif (3 < t) && (t < 6)
fee = 0.75;
elseif (6 < t) && (t < 24)
fee = 1.00;
elseif (t < 24) && (t < 48)
fee = 2.00;
elseif (t < 48) && (t < 72)
fee = 3.00;
elseif (t < 72) && (t < 96)
fee = 4.00;
elseif (t < 96) && (t < 120)
fee = 5.00;
elseif (t < 120) && (t < 144)
fee = 6.00;
elseif (t < 144) && (t < 168)
fee = 7.00;
disp('Illegal Parking');
My Main Script:
valid = true;
while valid
disp('Do you want to enter the amount of hours?')
answer = input('Enter 1 for YES and 0 for NO :');
if answer == 1
carNumber = input("Enter the car number: ");
t = input("Enter the amount of hours parked: ");
valid = false;

  1 Comment

Rik on 26 Oct 2020
Your function doesn't actually use any input and you don't actually output the fee. So your function is totally unrelated to the input the user provides. Your code with a bunch of elseif statements is correct, but you need to use the time as an input and the fee as an output.
(I would suggest using a NaN in the case of illegal parking, or use a negative value, that way you will always have an output to your function)

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Oct 2020
You need to pass t into ParkingFees() and not loop over t, and return fee
function fee = ParkingFees(t)
if t == 1
See if you can finish it now.


Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by