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

Vijayramanathan B.tech-EIE-118006077
편집: 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 CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by