Time table code using while loops

102 visualizaciones (últimos 30 días)
Jose Grimaldo
Jose Grimaldo el 21 de Oct. de 2019
Comentada: Brandon Heil el 4 de Oct. de 2021
Screenshot (156).png

Respuestas (2)

Mandy Downs
Mandy Downs el 15 de Feb. de 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 comentarios
Kemuel Roberts
Kemuel Roberts el 29 de Mzo. de 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 el 4 de Oct. de 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

Iniciar sesión para comentar.


David Hill
David Hill el 22 de Oct. de 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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by