In the previous chapter, we learned how to work with one-dimensional arrays, which represent a set of elements arranged in a single line. Now let's move on to more complex data structures - two-dimensional arrays.
A two-dimensional array represents a table or grid of elements that are organized in rows and columns. Each element of a two-dimensional array has two indices: one for the row and one for the column.
To declare a two-dimensional array, use the following syntax:
Let's look at an example of a two-dimensional array representing a 3x3 table with integers:
In this example, we declared a two-dimensional array matrix with three rows and three columns. Each element of the array has two indices:
For example, element matrix[0][0] is located in the upper left corner of the table.
We can assign values to elements of a two-dimensional array like this:
Now each element of the matrix array has its own value.
We can also get values of two-dimensional array elements using their indices:
The output will be as follows:
Thus, two-dimensional arrays allow us to store data in tabular form and conveniently access elements by their coordinates.
For working with two-dimensional arrays, it's very convenient to use nested loops:
You can also initialize a two-dimensional array immediately upon declaration:
Or in one line:
Two-dimensional arrays are often used for:
Two-dimensional arrays are a powerful tool for working with tabular data. They extend the capabilities of one-dimensional arrays, allowing you to organize data in rows and columns, making them ideal for many practical programming tasks.
data_type array_name[number_of_rows][number_of_columns];
int matrix[3][3];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
matrix[2][0] = 7;
matrix[2][1] = 8;
matrix[2][2] = 9;
cout << matrix[0][0] << " " << matrix[0][1] << " " << matrix[0][2] << endl;
cout << matrix[1][0] << " " << matrix[1][1] << " " << matrix[1][2] << endl;
cout << matrix[2][0] << " " << matrix[2][1] << " " << matrix[2][2] << endl;
1 2 3
4 5 6
7 8 9
#include <iostream>
using namespace std;
int main() {
int matrix[3][3];
// Filling the array using nested loops
int counter = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = counter;
counter++;
}
}
// Outputting the array using nested loops
cout << "Two-dimensional array 3x3:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl; // New line after each array row
}
return 0;
}
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
#include <iostream>
using namespace std;
int main() {
int matrix[2][3]; // 2 rows, 3 columns
// Input data
cout << "Enter elements of 2x3 array:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> matrix[i][j];
}
}
// Output data
cout << "\nEntered array:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int matrix[3][3] = {
{5, 12, 3},
{8, 25, 7},
{15, 9, 20}
};
int maxElement = matrix[0][0];
int maxRow = 0, maxCol = 0;
// Finding maximum element
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] > maxElement) {
maxElement = matrix[i][j];
maxRow = i;
maxCol = j;
}
}
}
cout << "Maximum element: " << maxElement << endl;
cout << "Position: [" << maxRow << "][" << maxCol << "]" << endl;
return 0;
}