Bird
Raised Fist0
Cprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is a two-dimensional array in C?
easy
A. An array of arrays, storing data in rows and columns
B. A single list of numbers stored in a line
C. A function that returns multiple values
D. A variable that holds only one value

Solution

  1. Step 1: Understand array structure

    A two-dimensional array stores data in a grid format with rows and columns.
  2. Step 2: Compare options

    Only An array of arrays, storing data in rows and columns describes this structure as an array of arrays.
  3. Final Answer:

    An array of arrays, storing data in rows and columns -> Option A
  4. Quick Check:

    2D array = array of arrays [OK]
Hint: Think of a table with rows and columns [OK]
Common Mistakes:
  • Confusing 2D arrays with single arrays
  • Thinking 2D arrays are functions
  • Assuming 2D arrays hold only one value
2. Which of the following is the correct way to declare a 3x3 integer two-dimensional array in C?
easy
A. int arr(3,3);
B. int arr[3][3];
C. int arr[3,3];
D. int arr{3}{3};

Solution

  1. Step 1: Recall C array declaration syntax

    In C, arrays use square brackets with sizes inside, like arr[rows][columns].
  2. Step 2: Check each option

    Only int arr[3][3]; uses correct syntax: int arr[3][3];
  3. Final Answer:

    int arr[3][3]; -> Option B
  4. Quick Check:

    Use square brackets for array sizes [OK]
Hint: Use square brackets for array sizes, not parentheses [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Using commas inside brackets
  • Using curly braces for declaration
3. What will be the output of the following C code?
int arr[2][2] = {{1, 2}, {3, 4}};
printf("%d", arr[1][0]);
medium
A. 3
B. 1
C. 2
D. 4

Solution

  1. Step 1: Understand array initialization

    The array arr has two rows: first row {1, 2}, second row {3, 4}.
  2. Step 2: Access element arr[1][0]

    Row index 1 is second row, column index 0 is first element in that row, which is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    arr[1][0] = 3 [OK]
Hint: Remember indexing starts at 0 for rows and columns [OK]
Common Mistakes:
  • Confusing row and column indexes
  • Starting index at 1 instead of 0
  • Mixing up elements in initialization
4. Identify the error in the following C code snippet:
int matrix[2][3];
matrix[2][1] = 5;
medium
A. Wrong data type for matrix
B. No error, code is correct
C. Syntax error due to missing semicolon
D. Index out of bounds error on matrix[2][1]

Solution

  1. Step 1: Check array size and valid indexes

    matrix is declared with 2 rows (0 and 1) and 3 columns (0 to 2).
  2. Step 2: Analyze matrix[2][1]

    Row index 2 is invalid because valid rows are 0 and 1 only, causing out of bounds error.
  3. Final Answer:

    Index out of bounds error on matrix[2][1] -> Option D
  4. Quick Check:

    Row index must be less than 2 [OK]
Hint: Array indexes start at 0 and go to size-1 [OK]
Common Mistakes:
  • Using index equal to array size
  • Ignoring zero-based indexing
  • Assuming no error for out-of-range access
5. Given the 2D array declaration int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};, which code snippet correctly calculates the sum of all elements?
hard
A. int sum=0; for(int i=1;i<3;i++) for(int j=1;j<3;j++) sum += grid[i][j]; printf("%d", sum);
B. int sum=0; for(int i=0;i<=3;i++) for(int j=0;j<=3;j++) sum += grid[i][j]; printf("%d", sum);
C. int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum += grid[i][j]; printf("%d", sum);
D. int sum=0; for(int i=0;i<3;i++) sum += grid[i]; printf("%d", sum);

Solution

  1. Step 1: Understand array size and loops

    grid has 3 rows and 3 columns, so indexes go from 0 to 2.
  2. Step 2: Check loop bounds and summation

    int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum += grid[i][j]; printf("%d", sum); uses correct loop bounds (i<3, j<3) and sums all elements correctly.
  3. Step 3: Analyze other options

    int sum=0; for(int i=0;i<=3;i++) for(int j=0;j<=3;j++) sum += grid[i][j]; printf("%d", sum); uses <=3 causing out-of-bounds access; the option starting loops at 1 misses first row and column elements; the option trying sum += grid[i] is invalid for 2D arrays.
  4. Final Answer:

    int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum += grid[i][j]; printf("%d", sum); -> Option C
  5. Quick Check:

    Use correct loop bounds and access grid[i][j] [OK]
Hint: Loop from 0 to size-1 for both rows and columns [OK]
Common Mistakes:
  • Using <= instead of < in loops
  • Starting loops at 1 instead of 0
  • Trying to add entire row instead of elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard