Time table code using while loops

102 views (last 30 days)
Jose Grimaldo
Jose Grimaldo on 21 Oct 2019
Commented: Brandon Heil on 4 Oct 2021
Screenshot (156).png

Answers (2)

Mandy Downs
Mandy Downs on 15 Feb 2021
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 Comments
Kemuel Roberts
Kemuel Roberts on 29 Mar 2021
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 on 4 Oct 2021
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

Sign in to comment.


David Hill
David Hill on 22 Oct 2019
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

Community Treasure Hunt

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

Start Hunting!