# Calculate taxi fare by giving multiple inputs and single output

754 views (last 30 days)
Divyang Jain on 25 May 2019
Commented: Walter Roberson on 14 Feb 2022
Write a function called taxi_fare that computes the fare of a taxi ride. It takes two inputs: the distance in kilometers (d) and the amount of wait time in minutes (t). The fare is calculated like this:
• the first km is \$5
• every additional km is \$2
• and every minute of waiting is \$0.25.
Once a km is started, it counts as a whole (Hint: consider the ceil built-in function). The same rule applies to wait times. You can assume that d >0 and t >= 0 but they are not necessarily integers. The function returns the fare in dollars. For example, a 3.5-km ride with 2.25 minutes of wait costs \$11.75. Note that loops and if-statements are neither necessary nor allowed.
Walter Roberson on 14 Feb 2022
You are going to write correct code so that your code passes automatic grading? That's what you are going to do about random input ?

Kalpesh Shah on 22 Sep 2019
Its a Simple Question. Use 'ceil' as suggested in hint and it works fine
function fare = taxi_fare(d,t)
d = ceil(d)
t = ceil(t)
fare=5+(2*(d-1))+(t*0.25)
Walter Roberson on 30 May 2021
Suppose you do
A = 1
A = 1
B = A * 5
B = 5
A = 2
A = 2
After that series of statements, what is B ?
B
B = 5
B did not change. When you wrote B = A * 5, you were not creating a formula for B: instead MATLAB copies the current value for A as of the time of the assignment and uses that to calculate B, and after that B has no knowledge of how it got to have the value it has.
If you were writing formulas instead of expressions, then consider the formula
B = B + 1
B = 6
As a formula, it would have to be interpreted as forcing B to be some value such that B and (B+1) were the same value. There are only three values such that B and (B+1) are the same value:
-inf == -inf + 1
ans = logical
1
inf == inf + 1
ans = logical
1
[nan, nan+1]
ans = 1×2
NaN NaN
I did not try to compare nan and nan+1 because comparisons with nan are always false.
If we understand
B = B + 1
as copying the current value of B, adding 1, and making the result the new value of B, then we get something that is very useful in programming. But if we think we are writing formulas, that
B = B + 1
is defining a formula saying that B and B + 1 must be the same value, then that is something that has very few uses.
Why does it matter? Well, if you define
fare = 5 + 2*(d-1) + 0.25*t;
and then afterwards change d and t, then if you are defining fare as a formula, it might make sense to change d and t afterwards -- but as the B = B + 1 case shows, expecting those to be formulas is often not very useful. If you instead understand it as copying the current values of d and t and using those to compute fare and then forgetting all information about how that exact value came to be, then when you change d and t afterwards, then those changes to d and t do not matter.
Furthermore, if we were interpreting
fare = 5 + 2*(d-1) + 0.25*t;
as being a formula relating fare to whatever value of d and t existed at the time we asked about the value of fare, then we would also have to understand
d = ceil(d);
as being a formula -- and that is a formula that could only be true if d is already an integer. If the input d were, for example, 3.8, then as a formula
3.8 == ceil(3.8)
would be false, and instead of doing something useful, you could only be telling MATLAB that the function as a whole only applies if d was already an integer... and then what would you expect MATLAB to do if the user wanted to calculate the fare for 3.8 miles ?

Vijay G on 17 Aug 2020
Edited: Vijay G on 17 Aug 2020
function fare = taxi_fare(x,y)
d = 5+(((ceil (x))-1)*2);
t = ((ceil(y))*.25);
fare = d+t;
end
letme know whether this helped you. Thanks

Raj on 27 May 2019
Seems quite straightforward. Where exactly are you having problem? Your formulation should look like this:
Fare=5+(2*(d-1))+(t*0.25) % Minimum fare of \$5 for first KM plus \$2 for every additional KM plus waiting time charge
Just pass the inputs d and t to the function named taxi_fare and use ceil on both before putting them in the above mentioned equation. You will get your fare which you can pass as output of the function. Good luck!!
Walter Roberson on 22 Sep 2019
Sejal Syed please explain what it was you tried, and what error you encountered.

Jagmeet Singh on 28 May 2019
@Stephen Cobeldick and @Raj
thank you very much for your support guys.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 20 Sep 2019
What is the purpose of the table T ? The question does not ask you to display anything.
The calculations should not round() any component: ceil() is more appropriate.

amjad khan on 5 Apr 2020
Edited: Walter Roberson on 5 Apr 2020
function fare=taxi_fare(distance,time)
distance=ceil(distance-1)
time=ceil(time)
fare=5*(2+(distance-1)+0.25*time
end
% "d"can be used instead of distance and "t" instead of time,
Walter Roberson on 13 May 2020
the first km is in the base price. You need to calculate "additional" kilometres, after the first, so you subtract the 1 initial kilometre

Arooba Ijaz on 1 May 2020
function [fare] =taxi_fare (d,t)
a=ceil(d)
b=ceil(t)
fare=(b*0.25)+((a-1)*2)+5
end

Roweida Bawab on 5 May 2020
function fare = taxi_fare(d, t)
d = ceil(d);
t = ceil(t);
fare = 5 + (d-1)*2 + t*0.25
end

muyiwabowen on 8 May 2020
function rare = taxi_fare(d,t)
rare = 5 + 2*ceil((d-1)) + 0.25*ceil(t);
end

AYUSH MISHRA on 25 May 2020
function fare= taxi_fare(d,t)
d=ceil(d); %d=total distance cover by taxi
t=ceil(t); %t=total time taken to complete distance
RD=ceil(d-1); %RD= remaning distace after 1 km distance
fare=5+2*RD+0.25*t;%fare=1st km charge + Remaning km Distance charge + waiting time charge
end
solution of question
fare=taxi_fare(3.5,2.25)
fare =
11.7500

Muhammad Talha Malik on 14 Jun 2020
Edited: Walter Roberson on 14 Jun 2020
can anyone tell me what is wrong with this code?
function fare = taxi_fare(d,t)
fare = (5+2*(d-1))+0.25*d;
end
Walter Roberson on 9 Feb 2021
@RAHWA ZESLUS by writing correct code?
What happens when you test your code with random input? Is it giving the same answer you get when you manually calculate for the same input?

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!