A two-dimensional array can represent a table, matrix, board, or map. It is an “array of arrays”: the first index selects a row and the second selects a column in that row. Both start at 0.
Creating and accessing
#include <iostream>
using namespace std;
int main() {
int grid[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
cout << grid[0][0] << endl; // 1
cout << grid[1][2] << endl; // 7
return 0;
}Row-column coordinate navigator
Select a value and watch the first index choose a row and the second choose a column.
KNOWLEDGE QUIZ
Rows and columns
Every row of a built-in two-dimensional array has the same column count. This example stores the dimensions in rows and cols; ragged structures require pointers or containers introduced later.
#include <iterator>
using namespace std;
int main() {
int triangle[3][3] = {
{1, 0, 0},
{1, 2, 0},
{1, 2, 3}
};
const size_t rows = size(triangle);
const size_t cols = size(triangle[0]);
return 0;
}KNOWLEDGE QUIZ
Nested loops
The outer loop controls rows and the inner loop controls columns. Use cols as the inner bound so the dimension is maintained in one place.
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
const int rows = 3;
int grid[rows][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
const int cols = 4;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
cout << setw(4) << grid[row][col];
}
cout << endl;
}
return 0;
}KNOWLEDGE QUIZ
Matrix addition
Matrix addition and subtraction require matching shapes. The result at position i,j uses only the elements at that same position in the two inputs.
int main() {
const int rows = 3;
const int cols = 2;
int a[rows][cols] = {{1, 3}, {1, 0}, {1, 2}};
int b[rows][cols] = {{0, 0}, {7, 5}, {2, 1}};
int result[rows][cols]{};
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
result[row][col] = a[row][col] + b[row][col];
}
}
return 0;
}Matrix addition, one cell at a time
The two input matrices stay on the left while the result starts empty. Each click computes exactly one matching cell.
C[0][0] = 1 + 0 = 1The purple cells mark the values read and written in this step.
KNOWLEDGE QUIZ
Statistics over a 2D array
First decide the scope: one row, one column, or the whole table. This code averages one row; converting to double avoids integer division.
int main() {
const int rows = 3;
int grid[rows][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
const int cols = 4;
int row = 1;
int sum = 0;
for (int col = 0; col < cols; col++) {
sum += grid[row][col];
}
double average = static_cast<double>(sum) / cols;
return 0;
}Add two matrices
Create two 3×2 int arrays, use nested loops to build their sum, and print the result as 3 rows and 2 columns. Do not write each sum manually.
- Store A, B, and result C in two-dimensional arrays.
- Use nested loops over the same row and column coordinates.
- Compute C[row][col] = A[row][col] + B[row][col] and print by rows.
Find the maximum in every row
Use {{4,9,2},{7,1,8},{3,6,5}}. Find the maximum in each row separately and print 9 8 6.
- Use the outer loop to process rows.
- Initialize row_max from the first element of each row.
- Compare remaining columns and print the maximum after each row.