Clear Filters
Clear Filters

Loops question,I think its for loops

1 view (last 30 days)
David Tejcek
David Tejcek on 2 Jul 2019
Commented: Alex Mcaulley on 3 Jul 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

Answers (1)

Alex Mcaulley
Alex Mcaulley on 2 Jul 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 Comments
Jan
Jan on 2 Jul 2019
Edited: Jan on 2 Jul 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 on 3 Jul 2019
Yes, you are right Jan. Following the statement, units == 1000 and units == 2000 are free XD

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!