0
0
Cprogramming~10 mins

Two-dimensional arrays in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Two-dimensional arrays
Declare 2D array
Initialize elements
Access element at row i, col j
Use nested loops to traverse
Perform operations on elements
End
This flow shows how a 2D array is declared, initialized, accessed by row and column, traversed with nested loops, and used for operations.
Execution Sample
C
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
for(int i=0; i<2; i++) {
  for(int j=0; j<3; j++) {
    printf("%d ", arr[i][j]);
  }
  printf("\n");
}
This code declares a 2x3 array, initializes it, and prints all elements row by row.
Execution Table
Stepi (row)j (col)Condition i<2Condition j<3ActionOutput
100TrueTruePrint arr[0][0] = 11
201TrueTruePrint arr[0][1] = 22
302TrueTruePrint arr[0][2] = 33
403TrueFalseEnd inner loop, print newline\n
510TrueTruePrint arr[1][0] = 44
611TrueTruePrint arr[1][1] = 55
712TrueTruePrint arr[1][2] = 66
813TrueFalseEnd inner loop, print newline\n
920FalseN/AEnd outer loop
💡 Outer loop ends when i=2, condition i<2 is False
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
i0000011112
j0012301230
Key Moments - 3 Insights
Why do we need two loops to traverse a 2D array?
Because a 2D array has rows and columns, the outer loop goes through rows (i), and the inner loop goes through columns (j), as shown in steps 1-8 in the execution_table.
What happens when the inner loop condition j<3 becomes false?
The inner loop ends, and the program prints a newline (step 4 and 8), then the outer loop increments i to move to the next row.
Why does the outer loop stop when i equals 2?
Because the array has 2 rows (indices 0 and 1), when i reaches 2, condition i<2 is false (step 9), so the loop ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of j after step 3?
A2
B3
C1
D0
💡 Hint
Check the 'j (col)' column in execution_table at step 3 and step 4.
At which step does the outer loop condition i<2 become false?
AStep 8
BStep 7
CStep 9
DStep 4
💡 Hint
Look at the 'Condition i<2' column in execution_table for the step where it is False.
If the array had 3 rows instead of 2, how would the execution_table change?
AThere would be more steps with i=2 iterating over j from 0 to 2
BThe inner loop would run fewer times
CThe outer loop would end earlier
DThe output would be empty
💡 Hint
Check how the outer loop variable i controls the number of rows in variable_tracker and execution_table.
Concept Snapshot
Two-dimensional arrays store data in rows and columns.
Declare with: type name[rows][cols];
Access with arr[i][j], where i=row, j=column.
Use nested loops: outer for rows, inner for columns.
Useful for matrices, grids, tables.
Indexing starts at 0.
Full Transcript
This lesson shows how two-dimensional arrays work in C. We start by declaring a 2D array with fixed rows and columns. Each element is accessed by two indexes: row and column. To process all elements, we use nested loops: the outer loop goes through each row, and the inner loop goes through each column in that row. The execution table traces each step of printing the array elements, showing how the indexes i and j change. Key moments clarify why two loops are needed, what happens when inner loop ends, and when the outer loop stops. The visual quiz tests understanding of loop variables and array size effects. The snapshot summarizes the syntax and usage of 2D arrays.