I need help with a loop.

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.

Réponses (2)

Cedric
Cedric le 27 Fév 2013
Modifié(e) : Cedric le 27 Fév 2013

0 votes

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
Youssef Khmou le 27 Fév 2013

0 votes

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 commentaires

Anna Bradley
Anna Bradley le 27 Fév 2013
Since this is for a class, it is required that we do it the way that our professor wants. She says that our new commands shoud be "two prompt/input to 1-start and 2-continue loop."
Youssef  Khmou
Youssef Khmou le 27 Fév 2013
ok i see, is it a homework? well then complete answer should be not given , only highlights
Youssef  Khmou
Youssef Khmou le 27 Fév 2013
Modifié(e) : Youssef Khmou le 27 Fév 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by