How to create a for/ while loop until a condition is met

I'm creating a function to determine the cost of parking at an airport. I have specific time requirements for the time parked and the related cost. My question is how can I change my time=input line to incorporate weeks, hours, days, and mins. Also for each lot there is a daily maximum which once you reach you can't be charge any more that day. How do I change my code to account for multiple days in the same lot? Please let me know if you have any questions about what I am asking.
Short Term Parking Lot
Duration Cost
| 0-30 min | Free (day 1 only)
| 31-60 min | $2.00
| Each additional hour | $1.00
| Daily maximum | $14.00
function parking_meter
%This function gives you the total cost of parking for airfair for 4 types of parking lots at Northwest Arkansas Regional
%To determine the cost of the parking the user will then select a parking
%lot
cost=menu('Select a parking lot to calculate your parking cost, a', 'Short Term','Intermediate', 'Long Term', 'Economy')
switch cost
case 1
time=input('input the duration you parked in minutes \n')
if 0<=time && time<=30
disp('Free for one day')
elseif 31<=time && time<=60
disp('$2.00')
elseif time > 60
a_cost=time./60*1;
cost= 2+a_cost;
disp(cost)
end
%How do you incorporate the daily maximum of $14 11 additonal hours
...........
end

1 Comment

This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Sign in to comment.

 Accepted Answer

Rik
Rik on 24 Apr 2019
Edited: Rik on 24 Apr 2019
You can use the min function to enforce a maximum outcome.
max_cost_allowed=14;
cost=min(cost,max_cost_allowed);
As a more general remark, it would make more sense to me to decouple the cost calculation from the input. That way you can more easily convert your code to a GUI if you want to and you are more free to play around with the input method.
function parking_cost
lot_type='Short Term';
a=input(['parking time in day,hour,minute format\n',...
'(you can skip leading zeros)\n'],'s');
b=regexp(a,'\d*','match');
c=cellfun(@str2double,b);
%fill c with zeros as needed
if numel(c)<3
c=[zeros(1,3-numel(c)) c];
end
time=24*60*c(1)+60*c(2)+c(3);
cost=parking_meter(lot_type,time);
if cost==0
fprintf('parking is free\n')
else
fprintf('total cost is $%.2f\n',cost)
end
end
function cost=parking_meter(lot_type,time)
%This function gives you the total cost of parking for airfair for 4 types
%of parking lots at Northwest Arkansas Regional
%types allowed: 'Short Term','Intermediate', 'Long Term', 'Economy'
%time should be entered in minutes
switch lot_type
case 'Short Term'
if 0<=time && time<=30
cost=0;
elseif 31<=time && time<=60
cost=2;
else
%first hour is $2, each additional is $1
cost=1+ceil(time/60);
nth_days=1+floor(time/(60*24));
%daily max $14
cost=min(cost,14*nth_days);
end
end
end

9 Comments

Is there anyway you could comment more on your lines of code so I could understand where/why you did what you did. I'm still new to MATLAB as you can tell. Thank you!
Which line do you have the most trouble understanding? The regular expression? That one creates a cell array with all groups of continuous digits (note that this prevents a decimal number to be added).
For the rest of the function I would suggest stepping through the code line by line to see the effect and reading the documentation for funtions you don't know.
Also, if my answer solved your issue, please mark it as accepted answer. If not, feel free to comment with your remaining issues.
I do not understand the regualr expression and the one that creates the cell.
So the offending line of code is the following:
b=regexp(a,'\d*','match');
This line will try to match the regular expression \d* on the contents of a. What that means is that it will try to match an unbroken sequence of digits (\d is a digit, * is repeat). The third input tells the function what to do: store the matches in one cell each. So there a cell array is created.
You could think of it this way: we're going to binarize the input array, marking a digit with 1 and other characters with 0. Then we are going to identify the runs and store each run in its own cell.
a='10 8 32';
binarized= [1 1 0 1 0 1 1];
run_number=[1 1 0 2 0 3 3];
c=cell(1,3);
c{1}=a(run_number==1);
c{2}=a(run_number==2);
c{3}=a(run_number==3);
The regexp function does something similar, but much more efficient. That is how the input from the user is split into cell elements.
Then we can use cellfun to apply the str2double function on each cell element to convert the cellstring array to a normal numeric array.
Okay, thank you. Were you giving me two options of functions or one that included two?
If I use the second function it only calculates the cost for one day what if they parked there two+ days
function cost=parking_meter(lot_type,time)
%This function gives you the total cost of parking for airfair for 4 types
%of parking lots at Northwest Arkansas Regional
%types allowed: 'Short Term','Intermediate', 'Long Term', 'Economy'
%time should be entered in minutes
switch lot_type
case 'Short Term'
if 0<=time && time<=30
cost=0;
elseif 31<=time && time<=60
cost=2;
else
%first hour is $2, each additional is $1
cost=1+ceil(time/60);
nth_days=1+floor(time/(60*24));
%daily max $14
cost=min(cost,14*nth_days);
end
case 'Intermediate'
if 0<=time && time<=30
cost=0;
elseif 31<=time && time<=60
cost=2;
else
%first hour is $2, each additional is $1
cost=1+ceil(time/60);
nth_days=1+floor(time/(60*24));
%daily max $8
cost=min(cost,8*nth_days);
end
case 'Long Term'
if 0<=time && time<=30
cost=0;
elseif 31<=time && time<=60
cost=2;
else
%first hour is $2, each additional is $1
cost=1+ceil(time/60);
nth_days=1+floor(time/(60*24));
%daily max $7
cost=min(cost,7*nth_days);
end
case 'Economy'
if 0<=time && time<=15
cost=0;
elseif 16<=time && time<=60
cost=2;
else
%first hour is $2, each additional is $1
cost=1+ceil(time/60);
nth_days=1+floor(time/(60*24));
%daily max $5
cost=min(cost,5*nth_days);
end
end
If you enter a period that is longer than a day, the nth_day variable will account for the number of days and adjust the maximum price accordingly. The time variable is in minutes, but nothing is stopping you from entering 24*60+5=1445 to get the price for 1 day and 5 minutes of parking.
The two functions I posted each have their own goal: the first gets user input, the second calculation the price. By splitting the functions like that it is easier to write modular code. As long as someone writes a function that gets user input, they can use the second function to calculate the price. And if you want to repeat this for another airport, you don't need to change the first function, but only the parking meter function.
I guess I haven't gotten the function to work then. What about the remaining lots?
I hard-coded the short term, but you can use something similar to what you had before with menu. I would suggest using a cell array to store the options and use that to convert the number into the char.
And if my code is not working, what input are you entering?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 24 Apr 2019

Commented:

Rik
on 25 Apr 2019

Community Treasure Hunt

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

Start Hunting!