What am I missing?

조회 수: 2 (최근 30일)
Ashlee Rogers
Ashlee Rogers 2020년 10월 26일
답변: Image Analyst 2020년 10월 26일
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;
else
disp('Illegal Parking');
break
end
end
My Main Script:
clc
clear
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: ");
ParkingFees;
else
valid = false;
end
end
  댓글 수: 1
Rik
Rik 2020년 10월 26일
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)

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 10월 26일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by