Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
how to fix the error?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 And here is the code
when I checked the grader is telling the problem is incorrect that means some error is there in the code which I am not getting.it will be helpful if anyone help me to get rid of the error.
if D<=1&&D>0
if A<=18 || A>=60
f=2;
dol = f*80/100;
fprintf('fare for the first mile is $2 with concession\n');
else
dol=2;
fprintf('fare for the first mile is $2 without concession\n');
end
elseif D>1
if D<=10&&(A<=18||A>=60)
dol = (2+(D-1)*0.25)*80/100;
fprintf('fare with concession for each additional mile upto total dist of 10 miles is 25 cents with concession\n');
else
dol = 2+(D-1)*0.25;
fprintf('fare without concession for each additional mile upto total dist of 10 miles is 25 cents with concession\n');
end
elseif D>10&&(A<=18||A>=60)
x=D-11;
dol=(2+10*0.25+x*0.10)*80/100;
fprintf('fare with concession for each additional mile above 10 miles is 10 cents with concession\n');
else
x=D-11;
dol=2+(10*0.25)+(x*0.10);
fprintf('fare without concession for each additional mile above 10 miles is 10 cents with concession\n');
end
end
4 comentarios
John D'Errico
el 15 de Mayo de 2018
READ MY ANSWER. It explains why a 20% reduction in fare is NOT the same thing as an 80% reduction in fare.
Respuestas (1)
John D'Errico
el 15 de Mayo de 2018
Editada: John D'Errico
el 15 de Mayo de 2018
Why do you think that 0.4 is correct?
The first mile is $2.00. If age is less than or equal to 18 (or at least 60), then the fare is reduced by 20%, so 80% of $2.00 is what you should return. (Thus 1.60, NOT 0.40.)
So what does your code do when D==1?
dol = 2*D*20/100;
So you are taking 20% of 2*D. WHY? That result is never indicated by the problem you were posed.
What were you told? When D <= 1, the fare is $2.00. That is true, UNLESS age is less than 18 or greater than or equal to 60.
So your tests might start like this:
if D <= 1
fare = 2;
if A <= 18 || A >= 60
fare = fare*(1-20/100);
end
elseif ...
There are of course similar problems with the remainder of your code, because it looks like you did not appreciate what a 20% REDUCTION in fare means.
Suppose you were told that item X (which normally costs $!.00) is on sale in the store for a 20% reduction in price? You would expect to pay $0.80. You would NOT expect to pay 20 cents. Well, you should not. And the clerk in the store would not let you get away with paying that price either.
3 comentarios
John D'Errico
el 15 de Mayo de 2018
Fix ALL of the cases where you did the same thing. As well, I suppose you want to use the variable dol, instead of fare as I did.
But to tell us that it is still showing an error, this tells us nothing, because you don't tell us what the error is.
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!