Using a while loop, what am I missing?

2 ビュー (過去 30 日間)
David Hughes
David Hughes 2015 年 7 月 7 日
回答済み: Jan 2015 年 7 月 7 日
The code works but does not print the first number. The range is 1 to 11 odd numbers only but I need number 1 to show in the list.
% Initialize the variables
sum1=0;
k = 1;
%Column Headers
fprintf('\tn \ty \n')
%Code to loop until k > 11, then stop and print results in columns
while k < 11
k = k+2;
sum1 = sum1 + y(k);
fprintf('\t%i \t%.1f \n',k,sum1)
end

採用された回答

Jan
Jan 2015 年 7 月 7 日
Perhaps:
% Initialize the variables
sum1 = 0;
k = 1;
%Column Headers
fprintf('\tn \ty \n')
%Code to loop until k > 11, then stop and print results in columns
while k < 11
sum1 = sum1 + y(k);
fprintf('\t%i \t%.1f \n',k,sum1)
k = k+2; % Move behind the calculation
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by