How do I properly combine "and", "or" and "if"

12 vues (au cours des 30 derniers jours)
JGraf
JGraf le 23 Fév 2017
Commenté : JGraf le 24 Fév 2017
Hi,
My function needs to calculate the cost of a train ticket. The first mile is $2. Each additional mile up to 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Children under or equal to 18 and seniors older or equal to 60 pay 80%. The inputs to the function are distance and age.
I have below code, but for some reason it doesn't take the discount into consideration.
function price=fare(d,a)
if 18<a<60 && d <=1
price=2
elseif 18<a<60 && 1<d<10
price=2+d*0.25
elseif 18<a<60 && d==10
price=2+9*0.25
elseif 18<a<60 && d>10
price=2+(9*0.25)+((d-10)*0.10)
elseif a<=18 || a>=60 && d <=1
price=2*0.80
elseif a<=18 || a>=60 && 1<d<10
price=(2+d*0.25)*0.80
elseif a<=18 || a>=60 && d==10
price=(2+9*0.25)*0.80
elseif a<=18 || a>=60 && d>10
price=(2+(9*0.25)+((d-10)*0.10))*0.80
end
  1 commentaire
Steven Lord
Steven Lord le 23 Fév 2017
To add to John and Jan's answers, a couple suggestions:
  • Brevity may be the soul of wit, but (slightly) longer names can help make code easier to understand.
You use the variable name price in your code. How easy is it to guess what that variable represents? Now consider the variable named d. What does that represent? You may know because you read the problem description, but wouldn't it be easier to guess if it were called something like distance?
  • If you use magic constants in your code, but they aren't actually constants, figuring out which values need to change can be difficult and/or time consuming.
Somewhat related to the previous suggestion, what would happen if the train company decided to increase the fare for miles 2-10 from 25 cents to 35 cents? If you used a variable named CostForMiles2To10 in your code, adjusting the fare in your code is a matter of a one character change (and where to make that change is obvious.)
CostForMiles2To10 = 0.25;
% becomes
CostForMiles2To10 = 0.35;
Is the variable name CostForMiles2To10 more to type than 0.25? Yes. But tab completion, copy-and-paste (of the variable name, not the variable value), or abbreviation (Cost2_10) can mitigate some of that while still helping you understand what you wrote days, weeks, months, or years after you wrote it.

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 23 Fév 2017
Modifié(e) : John D'Errico le 23 Fév 2017
Note that this code fragment ALWAYS returns true
18<a<60
since regardless of the value of a, (18<a) ALWAYS returns either 0 or 1, i.e., false or true. Are both 0 and 1 less than 60? Yes.
Just because you choose to use some nice mathematical short hand, does not mean that it is equivalent to
(18<a) && (a<60)
You use similar expressions repeatedly in your code. A bad idea.
  1 commentaire
JGraf
JGraf le 23 Fév 2017
Follow up question. I just added fprintf(%.2f, ....) and get two answers (4 and 3 for fare(4,44). Why is that? The correct answer is 3.
function price=fare(d,a)
if 18<a && a<60 && d <=1;
price=2;
elseif 18<a && a<60 && 1<d && d<10;
price=fprintf('%.2f',(2+d*0.25));

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 23 Fév 2017
Modifié(e) : Jan le 23 Fév 2017
The code can be simplified:
function price=fare(d,a)
if 18<a && a<60
if d <=1
price = 2
elseif d < 10 % 1 < d is excluded before already
price = 2+d*0.25
% elseif d==10 % Covered by d < 10 already
% price = 2+9*0.25
else % not test required: if d>=10
price = 2+(9*0.25) + ((d-10) * 0.10)
end
else % Test not required: a<=18 || a>=60
if d <=1
price = 2*0.80
elseif d < 10 % 1 < d is excluded before
price=(2+d*0.25)*0.80
% elseif d==10 % Covered by d > 10
% price=(2+9*0.25)*0.80
else % no test required: if d>=10
price = (2+(9*0.25)+((d-10)*0.10))*0.80
end
end
If you have tested for d <= 1, you can omit the test d > 1 in the following.
Or in short:
function price = fare(d, a)
if d <= 1
price2 = 2;
else
price2 = 2 + (min(9, d) * 0.25) + (max(0, d-10) * 0.10);
end
if 18 >= a || a >= 60
price2 = price2 * 0.80;
end
  4 commentaires
Jan
Jan le 24 Fév 2017
Modifié(e) : Jan le 24 Fév 2017
The best idea to find out, what's going on is to use the debugger: Set a breakpoint in the first line and step through your code line by line. Examine the type and contents of the variables in teh Workspace Browser or in the Command Window.
JGraf
JGraf le 24 Fév 2017
Thank you

Connectez-vous pour commenter.


JGraf
JGraf le 23 Fév 2017
Thank you

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by