Challenge - 5 Problems
Nested Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops with matrix assignment
What is the output of the following MATLAB code?
result = zeros(2,3);
for i = 1:2
for j = 1:3
result(i,j) = i + j;
end
end
resultMATLAB
result = zeros(2,3); for i = 1:2 for j = 1:3 result(i,j) = i + j; end end result
Attempts:
2 left
💡 Hint
Remember that MATLAB indices start at 1 and the loops assign i + j to each element.
✗ Incorrect
The outer loop runs i from 1 to 2, inner loop runs j from 1 to 3. Each element result(i,j) is assigned i + j. So first row is [1+1, 1+2, 1+3] = [2 3 4], second row is [2+1, 2+2, 2+3] = [3 4 5].
❓ Predict Output
intermediate2:00remaining
Nested loops with conditional break
What is the value of variable
count after running this MATLAB code?count = 0;
for i = 1:5
for j = 1:5
count = count + 1;
if i * j > 6
break;
end
end
end
countMATLAB
count = 0; for i = 1:5 for j = 1:5 count = count + 1; if i * j > 6 break; end end end count
Attempts:
2 left
💡 Hint
The inner loop breaks when i*j > 6, so not all inner iterations run fully. Note that count increments before the check.
✗ Incorrect
count increments before the break check. i=1: j=1-5 (5*1=5<=6), 5 times. i=2: j=1-4 (2*4=8>6 after inc), 4 times. i=3: j=1-3 (3*3=9>6), 3 times. i=4: j=1-2 (4*2=8>6), 2 times. i=5: j=1-2 (5*2=10>6), 2 times. Total: 5+4+3+2+2=16.
🔧 Debug
advanced2:00remaining
Identify the error in nested loops
What error does this MATLAB code produce when run?
for i = 1:3
for j = 1:3
disp(i + j
end
endMATLAB
for i = 1:3 for j = 1:3 disp(i + j end end
Attempts:
2 left
💡 Hint
Check the parentheses in the disp function call.
✗ Incorrect
The disp function call is missing a closing parenthesis, causing a syntax error.
🧠 Conceptual
advanced2:00remaining
Number of iterations in nested loops
How many times will the innermost statement run in this MATLAB code?
count = 0;
for i = 1:4
for j = i:4
count = count + 1;
end
end
countMATLAB
count = 0; for i = 1:4 for j = i:4 count = count + 1; end end count
Attempts:
2 left
💡 Hint
The inner loop starts at i and goes to 4, so the number of iterations decreases as i increases.
✗ Incorrect
For i=1, j=1..4 (4 iterations); i=2, j=2..4 (3 iterations); i=3, j=3..4 (2 iterations); i=4, j=4..4 (1 iteration). Total = 4+3+2+1=10.
🚀 Application
expert3:00remaining
Create a multiplication table matrix
Which MATLAB code correctly creates a 5x5 multiplication table matrix
M where M(i,j) = i*j using nested loops?Attempts:
2 left
💡 Hint
Remember MATLAB matrices are indexed by row and column, and multiplication table means product of indices.
✗ Incorrect
Option D correctly initializes a 5x5 matrix and assigns M(i,j) = i*j. Option D swaps indices but produces the same matrix since multiplication is commutative. However, strictly matching M(i,j)=i*j is D. Option D uses invalid indexing M(i+j). Option D sums indices instead of multiplying.