If statement problem (tip calculator)

Im having trouble with this problem. I basically have to create a script that will receive an input as a billing amount and party size with an output calculation of a tip based on two conditions. The billing amount and party size should be considered during the calculations; if the bill is less than $10.00 the tip will be $1.80 and if the party size if greater than 2 persons an additional %32 will be included, if the bill is between $10.00 and $60.00 the tip is %18 and if the party size if greater than 4 persons an additional $4.20 will be included, if the bill is above $60.00 the tip is %20 and if the party size if greater than 6 persons an additional %13.33 will be included

1 Commento

Stephen23
Stephen23 il 6 Giu 2018
Modificato: Stephen23 il 6 Giu 2018
@Erasmo Hinojosa: what is your question?
Note that your code concept will not work: you either need to use a loop with indexing, or get rid of the if's altogether and use logical indexing (better). if statements do not operate on different parts of vectors in the way that you are trying to use them.

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 6 Giu 2018
Modificato: Stephen23 il 6 Giu 2018
I would recommend that you use logical indexing. I will do the first one for you:
t = nan(size(x));
idx = x<10;
t(idx) = 1.8;
idx = ...
t(idx) = 1.8+x(idx)*0.32;
...
I was going to do the first two conditions for you, but the logic makes no sense: the second condition will never be reached, because the first condition already matches all values that could match the second condition, so there will never any any values that reach the second condition. The second condition is superfluous.

3 Commenti

sorry, we're just learning the basics right now, somehow we are supposed to do an if statement displaying the tip that has to be given, i fixed the first condition, thanks for your advise, ill try using that, but we havent learned about indexin
@Erasmo Hinojosa: the screenshot shows two vectors, each with four elements in them. What do each of the four elements represent: are these four different cases to calculate the tip for?
So far this is what i came up with...when I call the function in the command window, it works for the most part, i just have to see how to deal with inputs like 0 or negatives. Thanks for your help!

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by