Error: Unexpected MATLAB expression.
Show older comments
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
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
1 Comment
L F
on 18 Nov 2017
Thanks for answer
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
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.
Lars Wolff
on 7 Jun 2018
Thanks, Walter! I did it with round instead of ceil! Works!
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
Categories
Find more on Creating and Concatenating Matrices 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!