C++ Basics · Chapter 6

2D Arrays in C++: A Detailed Guide

Learn how to work with matrices, grids, and tabular data using two-dimensional arrays. Master nested loops, transposition, diagonal operations, and common matrix manipulations.

By Arj Updated July 2025 14 min read No prior experience needed
What you'll learn
  • What 2D arrays are and how they are structured
  • How to declare, initialize, and access elements
  • How to use nested loops for input and output
  • Common operations: sum, transpose, and diagonals
  • How to avoid common 2D array mistakes

1What is a 2D array?

A 2D array is simply an array of arrays. Think of it like a table with rows and columns. It is perfect for representing grids, matrices, game boards, or any data that naturally fits a two-dimensional structure.

Regular 1D array:

cpp
[1, 2, 3]

2D array (2 rows, 3 columns):

cpp
[
  [1, 2, 3],
  [4, 5, 6]
]

This array has 2 rows and 3 columns – we call it a 2x3 (2 by 3) array.

When to use 2D arrays
Use 2D arrays whenever your data has a natural tabular or grid structure: spreadsheets, game boards (chess, Tic-Tac-Toe), image pixels, or mathematical matrices.

2Declaring a 2D array

Syntax:

cpp
type arrayName[rows][columns];

Example – declaring a 2x3 integer matrix:

cpp
int matrix[2][3]; // 2 rows, 3 columns

The first dimension is the number of rows. The second dimension is the number of columns.

3Initializing a 2D array

Manual initialization

cpp
int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

Omitting the row size: You can omit the row size if you are providing all data.

cpp
int matrix[][3] = {
    {7, 8, 9},
    {10, 11, 12}
}; // compiler counts 2 rows
Always specify the column size
You can omit the row size, but the column size must always be specified. The compiler needs to know how many elements are in each row.

4Accessing elements

To access elements, you need two indices:

  • First index = row
  • Second index = column

Example:

cpp
cout << matrix[1][2]; // Prints 6 (2nd row, 3rd column)
Output6
Zero-based indexing
matrix[0][0] is the top-left corner. For a 2x3 array, valid row indices are 0 and 1, and valid column indices are 0, 1, and 2.

5Input and output with 2D arrays

Use nested loops to handle row-wise or column-wise operations. The outer loop iterates over rows, and the inner loop iterates over columns.

Example – input and output of a 2x3 matrix:

cpp
int matrix[2][3];

cout << "Enter values:\n";
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        cin >> matrix[i][j];
    }
}

cout << "You entered:\n";
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        cout << matrix[i][j] << " ";
    }
    cout << endl;
}
Sample runEnter values: 1 2 3 4 5 6 You entered: 1 2 3 4 5 6

6Example: Sum of all elements

This example calculates the total sum of all elements in a 3x3 matrix.

cpp
int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

int sum = 0;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        sum += matrix[i][j];
    }
}

cout << "Total sum: " << sum << endl;
OutputTotal sum: 45

7Example: Transpose of a matrix

The transpose of a matrix swaps rows with columns. If the original is 2x3, the transpose is 3x2.

Original (2x3):

cpp
1 2 3
4 5 6

Transpose (3x2):

cpp
1 4
2 5
3 6

Code:

cpp
int original[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

int transpose[3][2];

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        transpose[j][i] = original[i][j];
    }
}

// Output the transpose
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        cout << transpose[i][j] << " ";
    }
    cout << endl;
}
Output1 4 2 5 3 6

8Example: Diagonal elements of a square matrix

For a square matrix (same number of rows and columns), you can extract the main diagonal and secondary diagonal.

cpp
int matrix[3][3] = {
    {10, 20, 30},
    {40, 50, 60},
    {70, 80, 90}
};

// Primary diagonal: matrix[i][i]
cout << "Primary Diagonal: ";
for (int i = 0; i < 3; i++) {
    cout << matrix[i][i] << " ";
}

// Secondary diagonal: matrix[i][n-i-1]
cout << "\nSecondary Diagonal: ";
for (int i = 0; i < 3; i++) {
    cout << matrix[i][2 - i] << " ";
}
OutputPrimary Diagonal: 10 50 90 Secondary Diagonal: 30 50 70

9Common mistakes to avoid

1. Confusing row and column order
The correct syntax is array[rows][columns]. Mixing them up leads to accessing the wrong elements or going out of bounds.
2. Accessing out-of-bounds indexes
For a 2x3 array, valid row indices are 0 and 1, and valid column indices are 0, 1, and 2. Accessing matrix[2][0] or matrix[0][3] causes undefined behavior.
3. Forgetting to initialize all elements
Uninitialized 2D array elements contain garbage values. Always initialize before use, especially when summing or printing.
4. Nested loop bounds
Always use the correct bounds in your nested loops: outer loop up to rows, inner loop up to columns.

10Mini exercises

Try these yourself
  1. Create a 3x3 matrix and calculate the sum of each row individually.
  2. Write a program to find the largest element in a 2D array.
  3. Create a 4x4 identity matrix (1s on the diagonal, 0s everywhere else).
  4. Ask the user to enter a matrix and print it in reverse row order (last row first).

Hint: For the identity matrix, set matrix[i][i] = 1 and all other elements to 0. For reverse row order, iterate the outer loop from rows-1 down to 0.

11Quiz yourself

2D Arrays Quiz
Question 1 of 10
Coming up next
Input and Output in C++
Learn about standard input/output, file reading and writing, and formatting output in C++.
Continue to Chapter 7

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top