Borrar filtros
Borrar filtros

Loops question,I think its for loops

1 visualización (últimos 30 días)
David Tejcek
David Tejcek el 2 de Jul. de 2019
Comentada: Alex Mcaulley el 3 de Jul. de 2019
Hi everyone,
Could someone explain to me how to answer a question such as this:
Write a good which calculates the total cost of materials with one variable being units. If there are less than 1000 units made, the cost is 30c per unit. If there is more than 1000 units sold but less than 2000 units sold it is $100+ 10c per unit. If there is more than 2000 units sold it is $150 +5c per unit. Calculate the total cost of the units and display it as total cost.
Thanks

Respuestas (1)

Alex Mcaulley
Alex Mcaulley el 2 de Jul. de 2019
It is not difficult (No loops needed):
function cost = calculateCost(units)
if units < 1000
cost = %write the formula
elseif units > 2000
cost = %write the formula
else
cost = %write the formula
end
end
  2 comentarios
Jan
Jan el 2 de Jul. de 2019
Editada: Jan el 2 de Jul. de 2019
Almost. The exact formulation:
if units < 1000
% less than 1000 units
elseif units > 1000 & units < 2000
% more than 1000 units sold but less than 2000 units
elseif units > 2000
% more than 2000
end
Following the instructions, the code fails for units==1000 and ==2000.
"Calculate the total cost of the units and display it as total cost" - A funny formulation also: Can I display the total cost as every other than the total cost?
Nevertheless, Alex, I assume you solved, what was intented. +1
Alex Mcaulley
Alex Mcaulley el 3 de Jul. de 2019
Yes, you are right Jan. Following the statement, units == 1000 and units == 2000 are free XD

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by