Explain this while loop
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2013년 2월 26일
"count" and "index" are just variable names. You could have used XYZ and Chocolate_IceCream instead and MATLAB would not have cared.
채택된 답변
Azzi Abdelmalek
2013년 2월 26일
편집: Azzi Abdelmalek
2013년 2월 26일
numbers(1)=2
numbers(2)=4
numbers(3)=3
numbers(4)=-5 % before numbers becomes <0 the counter=3
댓글 수: 0
추가 답변 (1개)
Youssef Khmou
2013년 2월 26일
편집: Youssef Khmou
2013년 2월 26일
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"
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!