Error: Unexpected MATLAB expression.
2 views (last 30 days)
Show older comments
Radoslav Gagov
on 12 Mar 2017
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 Comments
mallavarapu hema saikumar
on 17 Feb 2018
Edited: mallavarapu hema saikumar
on 17 Feb 2018
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
on 15 Oct 2018
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.
Accepted Answer
Jorge Alberto Fuentes Casillas
on 11 Apr 2017
Edited: Jorge Alberto Fuentes Casillas
on 11 Apr 2017
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 :)
0 Comments
More Answers (4)
SAYANTAN BHANJA
on 28 Jun 2017
Edited: DGM
on 4 Mar 2023
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
on 10 Feb 2018
Edited: DGM
on 4 Mar 2023
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
0 Comments
Lars Wolff
on 6 Jun 2018
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 Comments
Walter Roberson
on 6 Jun 2018
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.
Duddela Sai Prashanth
on 23 Sep 2018
Edited: DGM
on 4 Mar 2023
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
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!