Calculate taxi fare by giving multiple inputs and single output
Info
This question is locked. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
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.
11 Kommentare
per isakson
am 25 Mai 2019
Homework? What have you done so far?
vignesh suresh
am 21 Feb. 2020
Bearbeitet: Walter Roberson
am 3 Apr. 2020
function fare= taxi_fare(d,t) d=ceil(d),t=ceil(t)
n=1; fare=((d-n)*2+(d/d)*5)+(t*0.25);
Indrajit Marathe
am 29 Apr. 2020
function fare= taxi_fare(d,t)
d=ceil(d);
t=ceil(t);
D=ceil(d-1);
fare=5+2*D+0.25*t;
end
Juhi Shrivastava
am 18 Mai 2020
What is the correct solution ?
Eyobel Gebre
am 17 Jun. 2020
yep!
Harsha Vardhan
am 25 Jul. 2020
Why is (d-1) taken?
Please help, I am a beginner in programming.
Walter Roberson
am 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
am 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
Portato Chu
am 1 Jan. 2021
Bearbeitet: Portato Chu
am 1 Jan. 2021
function fare = taxi_fare(d,t)
fare = distance(d)+waite(t);
end
function costdis = distance(M)
M = ceil(M);
costdis = (M-(M-1))*5 + ((M-1)*2)
end
function costime = waite(N)
N = ceil(N);
costime = N*0.25
end
SALMA HACHIMY
am 14 Feb. 2022
what about random input what we gonna do??
Walter Roberson
am 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 ?
Antworten (12)
Kalpesh Shah
am 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)
9 Kommentare
Risa Dwi Ratnasari
am 1 Dez. 2019
what is ceil mean?
This was discussed extensively in the comments to Raj's answer, so you could have found the answer to your question by simply reading this thread.
You could have also found the documentation yourself using any internet search engine.
Safir Iqbal
am 22 Mär. 2020
"ceil" is used to roundoff to nearest integer
Walter Roberson
am 22 Mär. 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
am 3 Apr. 2020
Bearbeitet: samyak doshi
am 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
Thato Mokhothu
am 11 Mär. 2021
I have tried this solution and it doesn't work
Rik
am 11 Mär. 2021
@Thato Mokhothu Then you should probably do your own homework.
Quyen Le
am 30 Mai 2021
@Thato Mokhothu idk why but you have to arrange like that way : func (1st row), ceil for 2nd and 3rd rows, then the equation in the forth row, then it works :)) i don't really get why it can not run when I let the equation on the 2nd row.
LIke this
function fare = taxi_fare (d,t)
d = ceil(d);
t = ceil(t);
fare = 5 + 2*(d-1) + 0.25*t;
end
Instead Of
function fare = taxi_fare (d,t)
fare = 5 + 2*(d-1) + 0.25*t;
d = ceil(d);
t = ceil(t);
end
Suppose you do
A = 1
B = A * 5
A = 2
After that series of statements, what is B ?
Answer:
B
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
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
inf == inf + 1
[nan, nan+1]
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 ?
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
am 27 Mai 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!!
18 Kommentare
Jagmeet Singh
am 27 Mai 2019
well i tried your solution, but it does not meet the criteria when distance is 3.5kms and time is 2.25 minutres.
Jagmeet Singh
am 27 Mai 2019
Raj,
your statement gives an answer of 10.5625 when distance d=3.5 and time t=2.25. Whereas in the question it is stated that the value should be 11.75. Could you please help on that. I am thinking maybe we need to round of d and t.
i tried but my statement could be incorrect or i might not be using the correct logic.
Thanks
Stephen23
am 27 Mai 2019
"I am thinking maybe we need to round of d and t."
Your assignment states "Hint: consider the ceil built-in function" Did you try that?
Show us the code that you tried.
Jagmeet Singh
am 27 Mai 2019
function fare=taxi_fare(d,t)
ceil(d)
ceil(t)
fare=5+(2*(d-1))+(t*0.25)
ceil(d)
calculates the ceiling of d , displays it in the command window (because this is the default if you do not suppress the output) and then simply discards that value. So you calcuated the ceiling of d but then did not use it in your calculation.
Note that very basic MATLAB concepts, such as how to call functions with outputs, are explained in the introductory tutorials:
Raj
am 27 Mai 2019
Hi,
I have tried the equation with when distance d=3.5 and time t=2.25 it gives the correct value as 11.75$. As explained by Stephen, you have to use the ceil values in your equation. Something like this:
Dist=ceil(Dist);
Wait_Time=ceil(Wait_Time)
Fare=5+(2*(Dist-1))+(Wait_Time*0.25)
Jagmeet Singh
am 28 Mai 2019
Verschoben: DGM
am 21 Feb. 2023
@Stephen Cobeldick and @Raj
thank you very much for your support guys.
Divyang Jain
am 29 Mai 2019
Tereza Vondrova
am 11 Jul. 2019
Bearbeitet: Tereza Vondrova
am 11 Jul. 2019
can you write the whole code down please, it is still not work for me :////
Swathi D
am 4 Sep. 2019
what does this ceil function do?
Walter Roberson
am 4 Sep. 2019
https://www.mathworks.com/help/matlab/ref/ceil.html
Stephen23
am 4 Sep. 2019
"what does this ceil function do?"
Selvakumar T
am 4 Sep. 2019
why d-1??
Walter Roberson
am 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.
Ivonne Ordoñez
am 20 Sep. 2019
Verschoben: DGM
am 21 Feb. 2023
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
kmadic = 2
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
am 20 Sep. 2019
Verschoben: DGM
am 21 Feb. 2023
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.
Sejal Syed
am 21 Sep. 2019
Doesnt work tried it
Walter Roberson
am 22 Sep. 2019
Sejal Syed please explain what it was you tried, and what error you encountered.
amjad khan
am 5 Apr. 2020
Bearbeitet: DGM
am 4 Mär. 2023
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,
3 Kommentare
Walter Roberson
am 5 Apr. 2020
I would not think that they want the temporary values such as distance to be displayed.
Jessika Lisboa
am 12 Mai 2020
Why it needs to be (distance-1)?? I dont understand, can you please help me?
Thank you
Walter Roberson
am 13 Mai 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
am 1 Mai 2020
function [fare] =taxi_fare (d,t)
a=ceil(d)
b=ceil(t)
fare=(b*0.25)+((a-1)*2)+5
end
AYUSH MISHRA
am 25 Mai 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
1 Kommentar
DGM
am 21 Feb. 2023
After the first line, d is an integer, so d-1 is also an integer. Using ceil() on d-1 does nothing.
I'll give you credit for using comments though. That's good.
Dante Gallo Torres
am 14 Jun. 2023
You can use matrix for this problem:
function fare = taxi_fare(d, t)
fare = [1 ceil(d-1) ceil(t)] * [5; 2; 0.25]
end
1 Kommentar
Rik
am 14 Jun. 2023
Neat solution to 'hide' the addition in the matrix product. The only things missing in your answer are documentation of this function and a semicolon.
Roweida Bawab
am 5 Mai 2020
function fare = taxi_fare(d, t)
d = ceil(d);
t = ceil(t);
fare = 5 + (d-1)*2 + t*0.25
end
muyiwabowen
am 8 Mai 2020
function rare = taxi_fare(d,t)
rare = 5 + 2*ceil((d-1)) + 0.25*ceil(t);
end
Muhammad Talha Malik
am 14 Jun. 2020
Bearbeitet: Walter Roberson
am 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
3 Kommentare
Walter Roberson
am 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
RAHWA ZESLUS
am 9 Feb. 2021
how can i solve rondom input?
Walter Roberson
am 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?
I wrote this code for the test, but somehow it isnt working with the test, i am getting the right answers but it isnt letting me pass, can anyone show what could be the problem
function total_taxi=taxi_fare(d,t)
total_taxi= 5+2*ceil(d-1)+0.25*ceil(t)
end
4 Kommentare
Walter Roberson
am 26 Jul. 2023
What feedback are you getting from the grader?
To confirm, you are asked to handle the same 5/2/0.25 as asked in the Question posted here?
Walter Roberson
am 26 Jul. 2023
Your calculation is wrong for distance values in the range 0 (exclusive) to [eps(1/4)+eps(eps(1/4))] . For values in that range, d-1 becomes -1 because of floating point double precision limitations . The question permits you to assume d>0 but a distance of (for example) 1 femtometre is > 0 and so fair game for the purposes of the question.
Stavan
am 26 Jul. 2023
okay so what should be written instead of -1 according to you
Also the grader just says the answer is incorrect, even though the code works and gives out correct answers
Walter Roberson
am 26 Jul. 2023
add (d>=1) .* kilometer_cost instead of kilometer_cost
Karan
am 25 Okt. 2023
I would say this code is more general and can be use for any km if not counted as a whole number, like one can use it for 0.5km.
function fare = taxi_fare(d, t)
d = ceil(d);
t = ceil(t);
fare = 5*(d - (d-1)) + (d-1)*2 + t*0.25
end
This question is locked.
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!