2D Arrays in C++ | A Detailed Guide

What You’ll Learn

By the end of this post, you’ll be able to:

  • Understand what a 2D array is
  • Declare, initialize, and access elements of a 2D array
  • Use nested loops to work with 2D arrays
  • Solve simple problems using 2D arrays like matrix input/output and element operations

What is a 2D Array?

A 2D array is simply an array of arrays. Think of it like a table with rows and columns.

If a regular array looks like this:

[1, 2, 3]

Then a 2D array could look like this:

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

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


Declaring a 2D Array

Syntax:

type arrayName[rows][columns];

Example:

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

Initializing a 2D Array

Manual Initialization:

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

You can also omit the row size if you’re providing all data:

int matrix[][3] = {
{7, 8, 9},
{10, 11, 12}
};

Accessing Elements

To access elements, you need two indices:

  • First index = row
  • Second index = column

Example:

cout << matrix[1][2]; // Prints 6 (2nd row, 3rd column

Arrays are zero-indexed, so matrix[0][0] is the top-left corner.


Input and Output with 2D Arrays

Use nested loops to handle row-wise or column-wise operations.

Example: Input and Output of a 2×3 matrix

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;
}

Example: Sum of All Elements

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;

Example: Transpose of a Matrix

The transpose of a matrix swaps rows with columns.

If this is the original:

1 2 3
4 5 6

The transpose will be:

1 4
2 5
3 6

Code:

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;
}

Example: Diagonal Elements of a Square Matrix

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] << " ";
}

Common Mistakes to Avoid

  • Confusing row and column order ([row][col])
  • Accessing out-of-bounds indexes
  • Forgetting to initialize all elements if required

Quiz: 2D Arrays

Question 1/7

Mini Exercises

  1. Create a 3×3 matrix and calculate the sum of each row.
  2. Write a program to find the largest element in a 2D array.
  3. Create a 4×4 identity matrix (1s on diagonal, 0s elsewhere).
  4. Ask the user to enter a matrix and print it in reverse row order.

Summary

In this post, you learned:

  • What 2D arrays are and how they are structured
  • How to declare, initialize, and access them
  • How to use nested loops for input/output
  • Common operations like sum, transpose, and diagonals

What’s Next?

In the next post, we’ll explore Input/Output in C++, including standard input/output, file reading and writing, and simple graphics.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top