Help with if, else if & else statements
Mostra commenti meno recenti
I am working on the following question for a class. 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.
So far I have written the following but when I run the debugger I keep getting messages about my ends not being in the correct location. I have fiddled with moving the code around with the if, else and else if statements, but I seem to be missing something.
function f = fare(dm,ay)
dm = round(dm);
if (dm <= 1)
f = 2;
else if (dm> 1 && dm >= 10)
f = 2 + (dm-1) * 0.25;
else if (m > 10)
f = 2 + ((dm-1) * 0.25) + ((dm-10)* 0.10);
if (ay <= 18 || ay >= 60)
p = f(.80);
else
p = f;
end
Risposta accettata
Più risposte (1)
Image Analyst
il 12 Gen 2019
0 voti
Ah, our old friend - the bus fare homework question.
Have you checked out all the other answers for this? Try that first before we go over it yet again.
Click for The Bus Fare Homework Problem
1 Commento
Hilary Gaiser
il 12 Gen 2019
Categorie
Scopri di più su Just for fun in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!