0
0
C++programming~10 mins

Multi-dimensional arrays in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multi-dimensional arrays
Declare multi-dimensional array
Access element by row and column indices
Modify or read element
Use nested loops to traverse rows and columns
Process or output elements
End
This flow shows how to declare, access, and traverse a multi-dimensional array using nested loops.
Execution Sample
C++
#include <iostream>
using namespace std;

int main() {
  int arr[2][3] = {{1,2,3},{4,5,6}};
  for(int i=0; i<2; i++) {
    for(int j=0; j<3; j++) {
      cout << arr[i][j] << ' ';
    }
    cout << '\n';
  }
  return 0;
}
This code declares a 2x3 array and prints all its elements row by row.
Execution Table
StepijCondition i<2Condition j<3ActionOutput
100TrueTruePrint arr[0][0] = 11
201TrueTruePrint arr[0][1] = 22
302TrueTruePrint arr[0][2] = 33
403TrueFalseInner loop ends, print newline\n
510TrueTruePrint arr[1][0] = 44
611TrueTruePrint arr[1][1] = 55
712TrueTruePrint arr[1][2] = 66
813TrueFalseInner loop ends, print newline\n
920FalseN/AOuter loop ends
💡 Outer loop i=2 is not less than 2, so loop ends.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5After Step 8Final
i000112
j00303N/A
Key Moments - 3 Insights
Why does the inner loop reset j to 0 after finishing one row?
Because the inner loop runs for each row, j must start at 0 to access columns from the beginning each time (see steps 4 and 5 in execution_table).
Why do we use two loops instead of one to access all elements?
A multi-dimensional array has rows and columns, so one loop goes through rows (i), and the inner loop goes through columns (j) to cover every element (see nested loops in code).
What happens if we try to access arr[2][0]?
It is out of bounds because the array has only 2 rows indexed 0 and 1. Accessing arr[2][0] causes undefined behavior (see exit condition i<2 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of j after step 4?
A2
B0
C3
D1
💡 Hint
Check the 'j' column in execution_table row for step 4.
At which step does the outer loop condition become false?
AStep 8
BStep 9
CStep 4
DStep 1
💡 Hint
Look at the 'Condition i<2' column in execution_table.
If the array size changed to arr[3][3], how many total elements would the loops print?
A9
B6
C3
D5
💡 Hint
Multiply the number of rows by columns to find total elements.
Concept Snapshot
Multi-dimensional arrays store data in rows and columns.
Declare with syntax: type name[rows][columns];
Access elements with arr[row][column].
Use nested loops to traverse all elements.
Outer loop for rows, inner loop for columns.
Indexing starts at 0 and goes to size-1.
Full Transcript
This lesson shows how multi-dimensional arrays work in C++. We declare an array with two dimensions, like int arr[2][3]. We access elements using two indices: row and column. To print all elements, we use nested loops: the outer loop goes through rows, and the inner loop goes through columns. Each loop runs until its condition is false. The inner loop resets its index after finishing a row. Trying to access outside the declared size causes errors. This visual trace shows each step of the loops and the values of variables i and j as they change.