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

Write a matlab function called fare that computes the bus fare one must pay in a

ID: 3835171 • Letter: W

Question

Write a matlab function called fare that computes the bus fare one must pay in a given city based on the distance travelled. Here is how the fare is calculated: the first mile is $2. Each additional mile up to a total trip distance of 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Miles are rounded to the nearest integer other than the first mile which must be paid in full once a journey begins. Children 18 or younger and seniors 60 or older get a 20% discount. The inputs to the function are the distance of the journey and the age of the passenger in this order. Return the fare in dollars, e.g., 2.75 would be the result returned for a 4-mile trip with no discount.

Explanation / Answer

% Below matlab function computes the bus fare one must pay in a given city based on the distace travelled.

function fun = fare ( Distance, Age)

if ( Distance > 0 && Distance < 1)

miles = 1;

else

miles = round (Distance);

end

temp = 0;

if (miles > 10)

temp = temp + (miles - 10) * 0.1;

miles = 10;

end

temp = temp + ((miles - 1) * 0.25) + 2;

if(Age <= 18 || Age >= 60) // check age of the passenger for discount

temp = temp * 0.8; % 20% discount

end

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote