Time table code using while loops

96 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by