Can someone explain this Loop?

1 visualización (últimos 30 días)
Tri Dang
Tri Dang el 14 de Jun. de 2021
Respondida: James Tursa el 14 de Jun. de 2021
num = 0;
L= randperm(10);
for k = L
while L(k)<4
num = num + 1;
L(k) = L(k)+1;
end
end
num = 6 is the answer
Can someone explain why it is 6? Not 3, 4 , or 5?

Respuesta aceptada

James Tursa
James Tursa el 14 de Jun. de 2021
Of the numbers 1 ... 10 that get produced by the randperm( ) call, only three of them are less than 4: 1, 2, 3.
1 will cause num to increase three times
2 will cause num to increase two times
3 will cause num to increase one time
three + two + one = 6

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by