Time table code using while loops

조회 수: 102 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by