Time table code using while loops

55 ビュー (過去 30 日間)
Jose Grimaldo
Jose Grimaldo 2019 年 10 月 21 日
コメント済み: Brandon Heil 2021 年 10 月 4 日
Screenshot (156).png

回答 (2 件)

Mandy Downs
Mandy Downs 2021 年 2 月 15 日
function outputMatrix = timesTable( sizeOfTable)
% Insert code to determine the size of the times table
sizeOfTable = abs(sizeOfTable);
sizeOfTable = floor (sizeOfTable);
rows = sizeOfTable;
columns = sizeOfTable;
% Insert code to initialize the table by filling all entries with zeros
outputMatrix (1:rows, 1:columns) = [0];
% Insert code to fill the first column and first row with consecutive integers
% starting with 1
i = 1;
while (i <= sizeOfTable)
outputMatrix (i, 1) = i;
outputMatrix (1, i) = i;
i = i + 1;
end
% Use a loop structure to fill the remaining entries in the times table
i = 1;
while (i <= sizeOfTable)
j = 1;
while (j <= sizeOfTable)
outputMatrix (i, j) = i * j;
j = j + 1;
end
i = i + 1;
end
end
  2 件のコメント
Kemuel Roberts
Kemuel Roberts 2021 年 3 月 29 日
this is good but it leaves all other elements in the array (except for 1st row and 1st column) with a value of zero.
Brandon Heil
Brandon Heil 2021 年 10 月 4 日
You can also do this I understand the assignment is asking for a while loop but thats very complicated compared to this.
function outputMatrix = timesTable(sizeOfTable)
sizeOfTable = abs(sizeOfTable);
A =[1:1:sizeOfTable];
B=A';
outPutMatrix = B*A

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


David Hill
David Hill 2019 年 10 月 22 日
Not sure why you have to use a while loop.
function T = timesTable(n)
T=1:n;
count=2;
while count<=n
T(count,:)=count*T(1,:);
count=count+1;
end

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by