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.

1 回表示 (過去 30 日間)
my code is
function [fare] = fare(a,d)
if d<=1
fare = 2;
elseif d<=10
fare = 2+0.25*(d-1);
else
fare = 2+0.10*(d-1);
return
end
if a<=18||a>=60
fare = 0.80*fare;
end
  1 件のコメント
John D'Errico
John D'Errico 2018 年 3 月 14 日
編集済み: John D'Errico 2018 年 3 月 14 日
Then what is your question? Is there a reason why you posted this?
Naming a function by the same name as the return variables would seem a bit dangerous. But surely there is some good reason why you are asking a question.

サインインしてコメントする。

回答 (1 件)

RAMAKANT SHAKYA
RAMAKANT SHAKYA 2019 年 2 月 8 日
function Kcost= fare(d, A)
d=round(d); %rounding the distance to nearest positive integer
if d <=1
Kcost=2; %for first kilometer
elseif d > 1 && d <= 10
Kcost=2+0.25*(d-1); % for distance greater then 1 and less than 10
elseif d > 10
Kcost=2+0.25*9+0.10*(d-10); % distance greater than 10
end
if (A<=18 || A>=60)
Kcost= 0.8*Kcost; %discount
end
end

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by