Error: Unexpected MATLAB expression.

1 回表示 (過去 30 日間)
Radoslav Gagov
Radoslav Gagov 2017 年 3 月 12 日
編集済み: DGM 2023 年 3 月 4 日
Hey guys. Can you help me a little bit with this code. I am not sure why it doesn't work.
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.
Here is my code :
function [ s ] = fare(m,a)
if m <=1;
fix(m)
P=2
elseif m > 1 && m <= 10;
fix(m);
p = 2 + 0.25 * (m-1)
elseif m > 10;
fix(m)
p = 2 + 9*0.25 + 0.10*(m-10)
end
if a <= 18 || a>= 60;
s = 0.8*p;
else
s=p;
end
end
  2 件のコメント
mallavarapu hema saikumar
mallavarapu hema saikumar 2018 年 2 月 17 日
編集済み: mallavarapu hema saikumar 2018 年 2 月 17 日
This will work
function p=fare(d,a)
b=round(d);
if (b<=0)
f=2;
elseif (b==1)
f=2;
elseif (1<b&& b<=10)
f=2+(b-1)*0.25;
elseif (b>10)
f = 4.25+(b-10)*0.1;
end
if (a<=18||a>=60)
p=0.8*f;
else
p=f;
end
Ashwin Raju
Ashwin Raju 2018 年 10 月 15 日
why 'if (b<=0)'? if b<=0, then the distance travelled is zero hence the price is also zero and no one can travel negative distance. Also round(d), rounds to the nearest decimal. For example round(0.5) will give 0 but here we want 1.

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

採用された回答

Jorge Alberto Fuentes Casillas
Jorge Alberto Fuentes Casillas 2017 年 4 月 11 日
編集済み: Jorge Alberto Fuentes Casillas 2017 年 4 月 11 日
Hello, as a first remark, I would like to tell you that if it is possible for you to improve the preview of your code, since right now it looks illegible.
Regarding the problem, you have to consider this:
1.- the distance can be from 0 to positive infinite, so you have to think on distances going from 0 to 1 km. Which will give a fare of 2.
2.-From the distances going from 2 to 10 km, you will pay: 2 + ((distance-1)*0.25)
3.-For distances above 11 km, then you must pay: 2+(9*0.25)+((distance-1)*0.1) --> or 4.25+((distance-1)*0.1)
NOTE: Remember that you must consider to round the distance to its closest integer. So you CAN'T use the "fix" function. Try with the "round" one, since for a 1.6 km distance, it should be rounded to a 2 km one.
Once you have calculated the cost-distance rate, calculate the possible discount:
4.-If age >= 60 or age <= 18, apply discount
5.-Else: no discount applied.
6.-Calculate the final fare to pay.
Enjoy :)

その他の回答 (4 件)

SAYANTAN BHANJA
SAYANTAN BHANJA 2017 年 6 月 28 日
編集済み: DGM 2023 年 3 月 4 日
function [TF]=fare(D,A)
D=round(D);
if(D<=1)
FR=2;
if(A<=17||A>=60)
TF=(FR-(FR/5))
else
TF=FR
end
elseif(D>1&&D<=10)
FR=2+(D-1)*(.25);
if(A<=18||A>=60)
TF=(FR-(FR/5))
else
TF=FR
end
else
FR=2+(9*.25)+(D-10)*(.10);
if(A<=18||A>=60)
TF=(FR-(FR/5))
else
TF=FR
end
end
end
OR U CAN USE RETURN COMMAND
  1 件のコメント
L F
L F 2017 年 11 月 18 日
Thanks for answer

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


Vijayramanathan B.tech-EIE-118006077
Vijayramanathan B.tech-EIE-118006077 2018 年 2 月 10 日
編集済み: DGM 2023 年 3 月 4 日
The easiest answer
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
end

Lars Wolff
Lars Wolff 2018 年 6 月 6 日
My solution would be:
function [price] = fare(distance, age)
d = (ceil(distance-1)+1)
if d <=1
x = 2
elseif d >1 && d <=10
x = (d-1)*0.25 + 2
elseif d >10
x = (d-10)*0.10 + 9*0.25 + 2
end
if ((age <= 18) || (age >= 60))
price = x*0.8
end
But the grader won't accept it:
Your selection: 2 NOTE: the grader will only determine if your solution for Problem 2 is correct or not. No score will be given.
Problem 2 (fare): Feedback: Your program made an error for argument(s) 4, 44
Your solution is _not_ correct.
Where is the bug? Thanks for your help!
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 6 日
Note that if the question you are asked is the same as the original question, then ceil() is not rounding to the nearest mile as is required by the question.
Anyhow:
You are not computing price unless the person is entitled to a discount.
Lars Wolff
Lars Wolff 2018 年 6 月 7 日
Thanks, Walter! I did it with round instead of ceil! Works!

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


Duddela Sai Prashanth
Duddela Sai Prashanth 2018 年 9 月 23 日
編集済み: DGM 2023 年 3 月 4 日
function dollar = fare(miles,age)
if miles <= 0
dollar = 0;
elseif miles > 0 && miles < 1
dollar = 2;
else
miles = round (miles-1);
dollar = 2;
if miles <10
dollar = dollar + (miles * 0.25);
else
dollar = dollar + (9 * 0.25) + ((miles - 9) * .10);
end
end
if age <=18 || age >=60
dollar = dollar - (dollar * 0.20);
end
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by