I would like to convert this code into a for loop to minimize the lines of code.
ID: 3802264 • Letter: I
Question
I would like to convert this code into a for loop to minimize the lines of code. IN MATLAB
E function v coin value (d,m) &Call; on this function by entering coin value (diameter (d),mass (m) SIf the entered diameter AND mass fall within 5$ of the ideal value you Swill get the correct coin and face value as a returned statement These first two statements will return an error if the input for diameter Sor mass are not a scalar value if length (d) 1 l I length (m) 1 error ('Error: Input must be a scalar for diameter and mass The first option is a penny with an ideal d 19.05mm and ideal m-2.50g elseif (d >J 18.5 & & d J 2.375 & & mExplanation / Answer
function v = coin_value(d, m)
if length(d) > 1 || length(m) > 1
error('Error:Inputs must be a scalar for diameter and mass')
end
diameter = [17.91,19.05, 21.21,24.26,30.61,26.5 ];
mass = [2.5, 2.5,5,6.25,11.34,8.10 ];
coin = {'Dime', 'Penny', 'Nickel', 'Quarter', 'Half-Dollar', 'Dollar' };
value = [0.10, 0.01, 0.05,0.25,0.50,1.00];
flag = 0;
for i = 1:length(d)
if (d >= diameter(i)*0.95 && d <= diameter(i)*1.05 && m >= mass(i) *0.95 && m <= mass(i)*1.05)
flag = 1;
v = value(i);
fprintf('Your coin is a %s and the value is $%4.2f ', coin{1}, v)
break;
end
end
if flag == 0
fprintf('We cannot determine the coin you have ')
end
end