← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 5 of 15

C++ Multi-dimensional Arrays

A multi-dimensional array is a table of values with rows and columns, like a chessboard or spreadsheet. You find an item with two numbers, row and column.
Syntax
cpp
data_type array_name[rows][columns];

data_type array_name[rows][columns] = {{v1, v2}, {v3, v4}};

array_name[row][column];

Two-Dimensional Arrays

A two-dimensional array models grid-shaped data — like a spreadsheet, a game board, or a matrix — by organizing values into rows and columns, and it's conceptually just an array where each element is itself another array.

Declaring int grid[3][4] creates 3 rows, each containing 4 columns.

Example: Two-Dimensional Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int grid[2][3];
	grid[0][0] = 1;
	// Print to the console with cout
	std::cout << grid[0][0] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Initializing 2D Arrays

Nested curly braces let you initialize a 2D array row by row, like int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};, which visually mirrors the actual grid structure and makes it obvious at a glance which values belong to which row.

Example: Initializing 2D Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
	// Print to the console with cout
	std::cout << grid[1][2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Elements in 2D Arrays

Reaching a specific element requires two indices in separate brackets, like grid[row][col], with both dimensions counted from zero just like a regular one-dimensional array.

Mixing up the row and column order is a common bug, especially when a grid isn't square.

Example: Accessing Elements in 2D Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
	// Print to the console with cout
	std::cout << grid[0][1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Elements in 2D Arrays

Updating a cell in a 2D array works exactly like a 1D array, just with two indices instead of one — grid[1][2] = 99; changes only that single coordinate, leaving every other element untouched.

Example: Modifying Elements in 2D Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
	grid[1][2] = 99;
	// Print to the console with cout
	std::cout << grid[1][2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Looping Through 2D Arrays

Visiting every element of a 2D array requires nested loops: the outer loop advances through rows one at a time, and for each row, the inner loop advances through every column in that row.

This nested structure directly mirrors the 2D array's own nested layout, which is why it feels like the natural way to traverse it.

Example: Looping Through 2D Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
	// Loop: repeat while the condition holds
	for (int row = 0; row < 2; row++) {
		// Loop: repeat while the condition holds
		for (int col = 0; col < 3; col++) {
			// Print to the console with cout
			std::cout << grid[row][col] << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Swapping row and column indexes, like a[col][row].
  2. Omitting the column size when passing a 2D array to a function, which is required.
  3. Assuming a partially initialized 2D array is random, when the missing elements become 0.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.