Loops question,I think its for loops

1 vue (au cours des 30 derniers jours)
David Tejcek
David Tejcek le 2 Juil 2019
Commenté : Alex Mcaulley le 3 Juil 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

Réponses (1)

Alex Mcaulley
Alex Mcaulley le 2 Juil 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 commentaires
Jan
Jan le 2 Juil 2019
Modifié(e) : Jan le 2 Juil 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 le 3 Juil 2019
Yes, you are right Jan. Following the statement, units == 1000 and units == 2000 are free XD

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by