Multi-dimensional Arrays
In this page:
2D Array Basics
A two-dimensional array stores data as rows and columns, effectively an array of arrays, which naturally represents grid-like data such as a spreadsheet, a game board, or an image's pixels.
Example: 2D Array Basics
#include <iostream>
using namespace std;
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}}; // rows and columns
cout << "grid[1][2]: " << grid[1][2] << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[][] grid = {{1, 2, 3}, {4, 5, 6}}; // rows and columns
System.out.println("grid[1][2]: " + grid[1][2]);
}
}
grid = [[1, 2, 3], [4, 5, 6]] # rows and columns
print("grid[1][2]:", grid[1][2])
#include <stdio.h>
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* rows and columns */
printf("grid[1][2]: %d\n", grid[1][2]);
return 0;
}
Login to try C/C++/Java code in the editor
Matrix Traversal
Visiting every cell of a 2D array normally uses two nested loops: the outer loop walks the rows, and the inner loop walks the columns within that row, giving you access to every element exactly once.
Example: Matrix Traversal
#include <iostream>
using namespace std;
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) cout << grid[i][j] << " ";
cout << endl;
}
return 0;
}
public class Main {
public static void main(String[] args) {
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) System.out.print(grid[i][j] + " ");
System.out.println();
}
}
}
grid = [[1, 2, 3], [4, 5, 6]]
for row in grid:
for val in row:
print(val, end=" ")
print()
#include <stdio.h>
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) printf("%d ", grid[i][j]);
printf("\n");
}
return 0;
}
Login to try C/C++/Java code in the editor
Matrix Calculations
You can perform elementwise operations like adding two same-sized matrices together, or reduce a matrix to a single value by summing every element across both loops as you traverse it.
Example: Matrix Calculations
#include <iostream>
using namespace std;
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2], total = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
total += sum[i][j];
}
cout << "sum[0][0]: " << sum[0][0] << ", Total of all cells: " << total << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{5, 6}, {7, 8}};
int[][] sum = new int[2][2];
int total = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
total += sum[i][j];
}
System.out.println("sum[0][0]: " + sum[0][0] + ", Total of all cells: " + total);
}
}
a = [[1, 2], [3, 4]]
b = [[5, 6], [7, 8]]
result = [[a[i][j] + b[i][j] for j in range(2)] for i in range(2)]
total = sum(sum(row) for row in result)
print("sum[0][0]:", result[0][0], ", Total of all cells:", total)
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2], total = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
total += sum[i][j];
}
printf("sum[0][0]: %d, Total of all cells: %d\n", sum[0][0], total);
return 0;
}
Login to try C/C++/Java code in the editor
Diagonal Operations
In a square matrix, the main diagonal consists of every cell where the row index equals the column index (matrix[i][i]), which comes up often in problems involving symmetry, identity matrices, or rotation.
Example: Diagonal Operations
#include <iostream>
using namespace std;
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int diagSum = 0;
for (int i = 0; i < 3; i++) diagSum += matrix[i][i]; // main diagonal
cout << "Main diagonal sum: " << diagSum << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int diagSum = 0;
for (int i = 0; i < 3; i++) diagSum += matrix[i][i]; // main diagonal
System.out.println("Main diagonal sum: " + diagSum);
}
}
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
diag_sum = sum(matrix[i][i] for i in range(3)) # main diagonal
print("Main diagonal sum:", diag_sum)
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int diagSum = 0;
for (int i = 0; i < 3; i++) diagSum += matrix[i][i]; /* main diagonal */
printf("Main diagonal sum: %d\n", diagSum);
return 0;
}
Login to try C/C++/Java code in the editor
Practical Matrix Problems
Matrix problems are excellent practice for nested-loop thinking, and common exercises include transposing a matrix (swapping rows and columns), rotating it 90 degrees, or searching for a value across the whole grid.
Example: Practical Matrix Problems
#include <iostream>
using namespace std;
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transposed[3][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
transposed[j][i] = matrix[i][j]; // swap rows and columns
cout << "transposed[2][1]: " << transposed[2][1] << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
int[][] transposed = new int[3][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
transposed[j][i] = matrix[i][j]; // swap rows and columns
System.out.println("transposed[2][1]: " + transposed[2][1]);
}
}
matrix = [[1, 2, 3], [4, 5, 6]]
transposed = [[matrix[i][j] for i in range(2)] for j in range(3)] # swap rows/cols
print("transposed[2][1]:", transposed[2][1])
#include <stdio.h>
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transposed[3][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
transposed[j][i] = matrix[i][j];
printf("transposed[2][1]: %d\n", transposed[2][1]);
return 0;
}
Login to try C/C++/Java code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: