# Calculate taxi fare by giving multiple inputs and single output

1,953 views (last 30 days)
Divyang Jain on 25 May 2019
Commented: Rik on 31 Aug 2020 at 8:36
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.

Harsha Vardhan on 25 Jul 2020
Why is (d-1) taken?
Walter Roberson on 25 Jul 2020
Harsha:
"every additional km" -- that means after the first km. The number of km after the first km is (d-1)
somnath paul on 6 Aug 2020
function fare= taxi_fare(d_km,t_sec)
d=ceil(d_km);
% d = ceil(d_km) rounds each element of of d_km to the nearest integer greater than or equal to that element.
t=ceil(t_sec);
D=ceil(d-1);
% I had to call another variable D because if i put d instead of D than it represent the input argument
% but we already taken 5 doller for the first kilometer. so, we need to eliminate that first kilometer.
fare=5+D*2+0.25*t;
end

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)

Safir Iqbal on 22 Mar 2020
"ceil" is used to roundoff to nearest integer
Walter Roberson on 22 Mar 2020
ceil does not mean to round off to nearest integer: that would be round()
ceil() means:
• if the value is already an integer, leave it as-is
• otherwise, return the next largest (closer to +infinity) representable integer
For example, -3.2 becomes -3 (next integer that is larger than the number), and +3.2 becomes +4 (next integer that is larger than the number), whereas "round to nearest integer" for +3.2 would be +3.
samyak doshi on 3 Apr 2020
use ceil function and make sure its ceil(d-1) not ceil(d) as first km is added differently
understand this code...thats more important than just copying it. its a basic code
function fare=taxi_fare(d,t)
D=ceil(d-1);
T=ceil(t);
fare=5+2*D+0.25*T;
end

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 4 Sep 2019
The first kilometer is a particular price. Additional kilometers are a different price. Additional kilometers beyond the first kilometer would be expressed by subtracting the first kilometer from the distance traveled to get additional kilometers.
Sejal Syed on 21 Sep 2019
Doesnt work tried it
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.

Ivonne Ordoñez on 20 Sep 2019
función tarifa = taxi_fare (d, t)
Recorridos=('primero'; 'segundo'; 'tercero')
d = (3.5; 3.1; 13)
y = redondo (d)
t = (2.25; 0; 0.6)
Y = redondo (t)
T=table(Recorridos,d,t)
primerkm=5
tesp = 0.25
tarifa = primerkm + ((Td) -1) * kmadic + Tt * .tesp
fin
% Yo tengo esto pero me marca un error "Expresión no válida. Al llamar a una función o indexar una variable, use paréntesis. De lo contrario, verifique si hay delimitadores no coincidentes.
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 5 Apr 2020
I would not think that they want the temporary values such as distance to be displayed.
Jessika Lisboa on 12 May 2020
Thank you
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

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

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

#### 1 Comment

Walter Roberson on 14 Jun 2020
You are multiplying the waiting time price 0.25 by the distance rather than the time waited
Waiting time needs to be calculated in whole minutes
kilometres needs to be calculated in whole kilometres.
If the distance is less than 1 km your code would use a negative value, d-1

shweta s on 2 Aug 2020
function [fare] =taxi_fare (dist,time)
p=ceil(dist)
q=ceil(time)
fare=(q*0.25)+((p-1)*2)+5
end

Khom Raj Thapa Magar on 31 Aug 2020 at 8:30
Taxi Fare
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.
Function =>>
function fare = taxi_fare(dis,time)
D = ceil(dis-1);
T = ceil(time);
fare = 5 + 2*D + 0.25*T;
end
Code to call my Function =>>
fare = taxi_fare(3.5,2.25)
Output::::::
fare =
11.7500

#### 1 Comment

Rik on 31 Aug 2020 at 8:36
Why are you posting solutions to homework questions? What will people learn from this? Matlab Answers is not a homework submission tool.

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by