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

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

ID: 2292414 • Letter: W

Question

Write a 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

MATLAB program:


clc;
clear all;
close all;

dist=input('Enter distance');
age=input('Enter age');
Min_fare=2;

fare=0;

fare=fare+Min_fare;
dist=round(dist)-1;

if dist-10<=0
fare=fare+0.25*dist;
else
fare= fare+0.25*10+0.1*(dist-10);
end

if age<=18 || age >= 60
fare=0.8*fare;
end

disp('Fare is :');
disp(fare);
  

Output:

Enter distance4
Enter age30
Fare is :
2.7500

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