Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In C/C++, we can simply define a multidimensional array as an array array. The data in a multidimensional array is stored as a table (in row principal order).

Declare the general form of an n-dimensional array:

data_type array_name[size1][size2].... [sizeN]; Data_type: Data type to be stored in an array. Array_name: the name of the array, size1, size2... ,sizeN: Size of dimensionCopy the code

Example:

Two-dimensional arrays:int two_d[10] [20]; 3d array:int3 _d [10] [20] [30];
Copy the code

The size of a multidimensional array

You can calculate the total number of elements that can be stored in a multidimensional array by multiplying the size of all dimensions. For example, the array int x[10][20] can store a total of (1020) = 200 elements. Similarly, the array int x[5][10][20] can store a total of (510*20) = 1000 elements.

2 d array

A two-dimensional array is the simplest form of a multidimensional array. To understand this, we can think of a two-dimensional array as an array of one-dimensional arrays.

  • Declare a two-dimensional array of size x, y.

    data_type array_name[x][y]; Data_type: indicates the type of data to be stored. A valid C/C++ data type.Copy the code
  • We can declare a two-dimensional array of integers, such as “x” of size 10,20:

    int x[10] [20];
    Copy the code
  • Elements in a two-dimensional array are usually referred to by x[I][j], where I is the row number and ‘j’ is the column number.

  • A two-dimensional array can be thought of as a table with “X” rows and “y” columns, with row numbers ranging from 0 to (x-1) and column numbers ranging from 0 to (y-1).

Initialize a TWO-DIMENSIONAL array: There are two ways to initialize a two-dimensional array. The first method:

int x[3] [4] = {0.1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
Copy the code

The array above has 3 rows and 4 columns. Left-to-right elements in braces are also stored in the table left-to-right. The elements are filled in the array in order, with the first four elements from the left in the first row, the last four elements in the second row, and so on.

A better way:

int x[3] [4] = {{0.1.2.3}, {4.5.6.7}, {8.9.10.11}};
Copy the code

This type of initialization uses nested braces. Curly braces represent one line in each group. In the example above there are three lines in total, so there are three sets of inner braces.

Accessing elements of a two-dimensional array: Use row and column indexes to access elements in a two-dimensional array. Example:

int x[2] [1];
Copy the code

The above example represents an element that exists in the third row, second column.

Note: In arrays, if the size of the array is N. It will be indexed from 0 to n-1. Therefore, for row index 2, the row number is 2+1 = 3.

To print all the elements of a two-dimensional array, we can use a nested for loop. We’re going to need two for loops. One iterates over rows and the other iterates over columns.

#include<iostream>
using namespace std;

int main(a)
{
	int x[3] [2] = {{0.1}, {2.3}, {4.5}};
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << "Element at x[" << i
				<< "] [" << j << "]."; cout << x[i][j]<<endl; }}return 0;
}
Copy the code

Output:

Element at x[0] [0] :0
Element at x[0] [1] :1
Element at x[1] [0] :2
Element at x[1] [1] :3
Element at x[2] [0] :4
Element at x[2] [1] :5
Copy the code

Initializing a THREE-DIMENSIONAL array: Initialization in a three-dimensional array is the same as initialization in a two-dimensional array. The difference is that as the number of dimensions increases, so does the number of nested braces. Method one:

int x[2] [3] [4] = {0.1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18 , 19.20.21.22.23};
Copy the code

A better way:

int x[2] [3] [4] = {{{0.1.2.3}, {4.5.6.7}, {8.9.10.11}}, {{12.13 ,14.15}, {16.17.18.19}, {20.21.22.23}}};Copy the code

Accessing elements in a THREE-DIMENSIONAL array: Accessing elements in a three-dimensional array is similar to accessing elements in a two-dimensional array. The difference is that we must use three loops instead of two to implement one dimension in a three-dimensional array.

#include<iostream>
using namespace std;

int main(a)
{
	int x[2] [3] [2] = {{{0.1}, {2.3}, {4.5}}, {{6.7}, {8.9}, {10.11}}};for (int i = 0; i < 2; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			for (int k = 0; k < 2; ++k)
			{
				cout << "Element at x[" << i << "] [" << j
					<< "] [" << k << "] ="<< x[i][j][k] << endl; }}}return 0;
}
Copy the code

Output:

Element at x[0] [0] [0] = 0
Element at x[0] [0] [1] = 1
Element at x[0] [1] [0] = 2
Element at x[0] [1] [1] = 3
Element at x[0] [2] [0] = 4
Element at x[0] [2] [1] = 5
Element at x[1] [0] [0] = 6
Element at x[1] [0] [1] = 7
Element at x[1] [1] [0] = 8
Element at x[1] [1] [1] = 9
Element at x[1] [2] [0] = 10
Element at x[1] [2] [1] = 11
Copy the code

In a similar way, we can create arrays of any number of dimensions. However, as the number of dimensions increases, so does the complexity. The most common multidimensional array is the two-dimensional array.