How do I subtract from specific elements in a vector?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jennifer Coss
el 8 de Mzo. de 2017
Editada: Rahul Kalampattel
el 8 de Mzo. de 2017
I'm supposed to be able to calculate the total cost of movie tickets, with a discount program. The program says that if there are more than 5 customers of a type (child, adult, senior, student) one person of the type gets in free. I think I'm really close to solving it, but I'm stuck at the last little part.
function [income cnt] = tickets( price, numTickets )
if(numTickets>5)
numTickets = (numTickets>5)-1;
end
income = price.*numTickets;
income = sum(income);
cnt = sum(numTickets);
end
So I added everything up myself (using [totalIncome totalCount]= tickets([3 10 6 5], [1 6 1 2])) and got 69 for the final price, but Matlab gave me 79.
Thanks for any help!
0 comentarios
Respuesta aceptada
Rahul Kalampattel
el 8 de Mzo. de 2017
Editada: Rahul Kalampattel
el 8 de Mzo. de 2017
function [income cnt] = tickets( price, numTickets )
cnt = sum(numTickets);
numTickets = numTickets - (numTickets>5);
income = sum(price.*numTickets);
end
If numTickets = [1 6 1 2], then (numTickets>5) == [0 1 0 0]. Subtract this from the original input, and you get the correct number of people to calculate the income with.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!