I need help with a loop.
Show older comments
The program that I am creating needs to go through all of the steps for multiple people until 0 is entered.
For example:
The height and weight will be entered for multiple people and will output multiple BMI's until zero is entered, and then the program will stop, but I don't know how to allow multiple people to input their data.
1 Comment
Azzi Abdelmalek
on 27 Feb 2013
What do you mean?
Answers (2)
You could start with something like:
personCnt = 0 ;
while true
fprintf('\n--- Person %d\n', personCnt+1) ;
height = input('Enter your height : ') ;
if height == 0, break ; end
weight = input('Enter your weight : ') ;
if weight == 0, break ; end
personCnt = personCnt + 1 ;
% Compute/store/output BMI.
end
Youssef Khmou
on 27 Feb 2013
hi Anna , there are many ways to control the inputs :
try this :
index=1;
a=rand(1);
b=rand(1);
while (a~=0) ||(b~=0)
fprintf('enter height of individual %i in cm\n',index);
a=input('');
fprintf('enter weight of individual %i in Kg\n',index);
b=input('');
index=index+1;
BMI=rand(1) % You enter the BMI formula here
end
Now you can create matrix A(m,n) to store the inputs for statistical analysis ,
3 Comments
Anna Bradley
on 27 Feb 2013
Youssef Khmou
on 27 Feb 2013
ok i see, is it a homework? well then complete answer should be not given , only highlights
Youssef Khmou
on 27 Feb 2013
Edited: Youssef Khmou
on 27 Feb 2013
hi , first i made a mistake using the OR operator , i had to use && instead , you had to be aware of that then, ok you try this enhanced version :
fprintf(' Welcome to Anna''s program : ) \n');
fprintf(' Enter :\n\t\t1.start\n\t\t3.exit\n');
I=input('');
if I==1
index=1;
a=rand(1);
b=rand(1);
while ((a~=0.00) && (b~=0.00))
fprintf('enter height of individual %i in cm\n',index);
a=input('');
fprintf('enter weight of individual %i in Kg\n',index);
b=input('');
index=index+1;
BMI=rand(1) % You enter the BMI formula here
end
fprintf(' Zeros value entered : Program exits');
elseif I==3
;
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!