フィルターのクリア

Nested Loop Script (Triangle)

23 ビュー (過去 30 日間)
Juan Zegarra
Juan Zegarra 2019 年 4 月 28 日
編集済み: Sulaymon Eshkabilov 2019 年 5 月 6 日
Write a nested llop script that will print the following pattern, printing one number or character at a time starting at 1 and using the numbers 0 through 9Screen Shot 2019-04-28 at 1.24.24 PM.png
  3 件のコメント
Juan Zegarra
Juan Zegarra 2019 年 4 月 28 日
This but im not sure what to print or how to do it for the first one.
for n=1:5
for s=1:n
fprintf('')
end
fprintf('\n')
end
Cedric
Cedric 2019 年 4 月 28 日
編集済み: Cedric 2019 年 4 月 28 日
It is a good start, you can try to display a charater followed by a space, just to make sure that your nested loops operate as desired:
for n = 1 : 5
for m = 1 : n
fprintf('A ') ;
end
fprintf('\n') ;
end
which outputs:
A
A A
A A A
A A A A
A A A A A
It looks like it is working. Now you solved one part of the problem, and there are many ways to solve the rest.
What happens if you keep incrementing some variable within the inner loop?
s = 1 ;
for n = 1 : 5
for m = 1 : n
fprintf('A ') ; % What about displaying the value of s instead of 'A '?
s = s + 1 ;
end
fprintf('\n') ;
end
Are you able to print the value of s instead of this character A? Maybe then you'll want to read about the modulo operation.

サインインしてコメントする。

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 6 日
編集済み: Sulaymon Eshkabilov 2019 年 5 月 6 日
Here is the complete answer to all parts of the assignment:
%%
clearvars; clc
%% Part 1.
k=1;
for ii = 1 : 5
for jj = 1 : ii
if k >10
k=k-10;
fprintf(num2str(k));
else
fprintf(num2str(k)) ;
end
k = k+1;
end
fprintf('\n') ;
end
%% Part 2
k = 65 ;
for ii = 1 : 5
for jj = 1 : ii
fprintf(char(k)) ;
k = k + 1 ;
end
fprintf('\n') ;
end
%% Part 3. Numbers
k=5;
for ii = 1 : 5
for jj = 1 : ii
if k >10
k=k-10;
fprintf(num2str(k));
else
fprintf(num2str(k)) ;
end
k = k+1;
end
fprintf('\n') ;
end
%% Part 3. Letters
k = 68 ;
for ii = 1 : 5
for jj = 1 : ii
fprintf(char(k) ) ;
k = k + 1 ;
end
fprintf('\n') ;
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