How to solve my code for the question given below, i have tried my level best with my code

1 回表示 (過去 30 日間)
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.
function amount = fare(distance,age)
amount=0;
if distance==0
amount=0;
else
amount=2;
end
if fix(distance) <=10
amount=amount+(0.25*(fix(distance)-1));
end
if fix(distance) > 10
amount=amount+(0.10*(fix(distance)-10));
end
if age <=18 && age >= 60
discount=0.20*amount;
amount=amount-discount;
end
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 2 月 10 日
What difficulty are you encountering?
Vijayramanathan B.tech-EIE-118006077
Vijayramanathan B.tech-EIE-118006077 2018 年 2 月 10 日
No walter! I got my output with this code I developed and it matches all of my test cases!
function amount = fare(distance,age)
amount=0;
if distance>0
amount=2;
else
amount=0;
end
if round(distance) <=10 && distance>1
amount=amount+(0.25*(round(distance)-1));
end
if round(distance) > 10
amount=2.25+amount+(0.10*(round(distance)-10));
end
if age <=18 || age >= 60
discount=0.20*amount;
amount=amount-discount;
end

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 10 日
Your line
if age <=18 && age >= 60
can only be true if age is simultaneously no more than 18 and is also at least 60.
I have seen philosophical arguments that "infinity" is simultaneously positive and negative, so there could maybe be some claim that someone who has reached the age of "infinity" is both less than 18 and more than 60, but other than that I am not aware of any way your test could be satisfied.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by