Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now, it\'s your turn. Write a function that computes parking fees for a garage w

ID: 3815145 • Letter: N

Question

Now, it's your turn. Write a function that computes parking fees for a garage with the fee structure given below. Your function should be named parking_fee, take a single input argument (hours parked in garage), and return a single output argument (total fee for parking). A lost ticket is signified using a negative value for hours. Partial hours are charged as full hours, e.g. 5 hours and 20 minutes is charged as 6 hours. Thus, the output should always be an integer. Examples (understand the PARKING FEES): parked for 2 hours, pay $4 parked for 3 hours, pay $7 parked for 4 hours, pay $7 parked for 5 hours, pay $8 parked for 20 hours, pay $23 parked for 22 hours, pay $24 parked for 123 hours, pay $127 parked for 123 hours but lost the ticket, pay $163 This is NOT an easy task. You MUST think before you code. You should discuss with your team how to approach the problem and sketch out a plan (e.g. in pseudocode or flow diagram) for how to solve it. This task may take you 20 to 45 minutes. It will take much longer if you do not think before you code. You have been provided with a script that will automatically test your function for partial correctness (test_parking_fee.m on eCampus). You should run the script right now and note the errors or failures. Pick an error or a failed test, track it down, fix it, and then nun the test script again. This is what is known as test-driven development (TDD): Write the tests first, then write the code to pass the tests.

Explanation / Answer

function fees = parking_fees(hours)
fees = 0;
  
% lost ticket fee and makin hour positive
if hours < 0
fees = fees + 36;
hours = abs(hours);
end
  
days = 0;
% if car is parked for more than 21 hours then we have to stick to daily max charge of 24
if hours > 21
% if parked for less than a day but more than 21 hours then fees will be 24 (4+3+17 = 24)
if hours < 24
hours = 21;
else
% take number of days car is parked and remianing hours
days = idivide(hours, 24);
hours = mod(hours, 24);
if hours > 21
hours = 21;
end
end
end
  
% compute fees
if hours <= 2
fees = fees + 4;
elseif hours <= 4
fees = fees + 7;
else
fees = fees + 7 + (hours - 4);
end
  
fees = fees + days*24;
end

fprintf("Parked for %d hours, pay $%d ", 2, parking_fees(2))
fprintf("Parked for %d hours, pay $%d ", 3, parking_fees(3))
fprintf("Parked for %d hours, pay $%d ", 4, parking_fees(4))
fprintf("Parked for %d hours, pay $%d ", 5, parking_fees(5))
fprintf("Parked for %d hours, pay $%d ", 20, parking_fees(20))
fprintf("Parked for %d hours, pay $%d ", 22, parking_fees(22))
fprintf("Parked for %d hours, pay $%d ", 123, parking_fees(123))
fprintf("Parked for %d hours but lost the ticket, pay $%d ", 123, parking_fees(-123))

Sample output

Parked for 2 hours, pay $4   
Parked for 3 hours, pay $7   
Parked for 4 hours, pay $7   
Parked for 5 hours, pay $8   
Parked for 20 hours, pay $23   
Parked for 22 hours, pay $24   
Parked for 123 hours, pay $127
Parked for 123 hours but lost the ticket, pay $163

Please rate positively if this solved your query

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote