Write a MATLAB script that asks a group of up to 50 members to enter their heigh
ID: 3939217 • Letter: W
Question
Write a MATLAB script that asks a group of up to 50 members to enter their height (in cm) and their weight (in kg). The script should work as follows: Prompt the user to enter the two values If any of the input values is 0, the program stops asking for more data Read in the input from the user, storing it in an appropriate variable Repeat steps for the next member, stop when the 50th member is done Extend the previous script so that it calculates the body mass index (BMI) of each member. It is defined by the formula BMI = Weight(kg)/Height(m)^2 If the BMI is in the range 25Explanation / Answer
a)
x=[];
y=[];
prompt1 = 'Enter your height: ';
prompt2 = 'Enter your weight: ';
%loops from 1 to 50
for i=1:50
% prompt for height
x(i)= input(prompt1);
% checks for 0 value
if x(i)==0
break
% breaks loop if true
end
% asks for weight
y(i)=input(prompt2);
if y(i)==0
%checks for 0 value
break
% breaks the loop if 0
end
end% for loop ends
b)
x=[];
y=[];
bmi=[];
prompt1 = 'Enter your height: ';
prompt2 = 'Enter your weight: ';
%loops from 1 to 50
for i=1:50
% prompt for height
x(i)= input(prompt1);
% checks for 0 value
if x(i)==0
break
% breaks loop if true
end
% asks for weight
y(i)=input(prompt2);
if y(i)==0
%checks for 0 value
break
% breaks the loop if 0
end
% bmi check
bmi(i)=y(i)/((x(i)/100)*(x(i)/100));
if (bmi(i) >= 25) && (bmi(i) < 30)
fprintf('your bmi is %d and your category is overweight ',bmi(i))
elseif bmi(i) >= 30
fprintf('your bmi is %d and your category is obese ',bmi(i))
end
end% for loop ends
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.