Explain this while loop

1 Ansicht (letzte 30 Tage)
Dave Smith
Dave Smith am 26 Feb. 2013
Please explain the steps to how this code fragment works. I don't really understand count or index. Thanks.
What is count as a result of the given Matlab code fragment? Showing your work
may result in partial credit.
numbers = [2 4 3 -5 1 0];
count = 0;
index = 1;
while numbers(index) > 0
count = count + 1;
index = index +1;
end
count = 3
  1 Kommentar
Walter Roberson
Walter Roberson am 26 Feb. 2013
"count" and "index" are just variable names. You could have used XYZ and Chocolate_IceCream instead and MATLAB would not have cared.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 26 Feb. 2013
Bearbeitet: Azzi Abdelmalek am 26 Feb. 2013
numbers(1)=2
numbers(2)=4
numbers(3)=3
numbers(4)=-5 % before numbers becomes <0 the counter=3

Weitere Antworten (1)

Youssef  Khmou
Youssef Khmou am 26 Feb. 2013
Bearbeitet: Youssef Khmou am 26 Feb. 2013
hi Dave :
Literally " Given a vector of length N, count the number of positive elements before the first negative one, as long as the numbers are positive count them but when a <0 number is met, stop the counting"

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by