Arrays. Part 2. Two-dimensional arrays. Arrays of strings. Multidimensional arrays
Content
- 1. Two-dimensional arrays. Examples of definition and use
- 2. Initialization of two-dimensional arrays. Example
- 3. Initialization of arrays of strings. Examples
- 4. Multidimensional arrays. Examples of definition and use
- 5. Initializing of multidimensional arrays. Example
- Related topics
Search other websites:
1. Two-dimensional arrays. Examples of definition and use
In C++, it is possible to use two-dimensional and multi-dimensional arrays. The two-dimensional array – is a list of one-dimensional arrays.
To access the elements of a two-dimensional array you need to specify two indexes. If we consider the array as a table, then the first index specifies the row. The second index specifies the column in the table.
Example 1. Definition of two-dimensional array of integers Matr, size 3*4.
int Matr[3][4]; // two-dimensional array, size 3*4
Access to the array elements (Figure 1):
Matr[0][0] = 23; Matr[2][3] = 41; Matr[1][2] = -8;
Figure 1. Access to the elements of the matrix Matr
To zeroing of two-dimensional array Matr need to write the following code:
... int Matr[3][4]; // two-dimensional array, size 3*4 int i,j; for (i=0; i<3; i++) for (j=0; j<4; j++) Matr[i][j] = 0; ...
Example 2. Definition of two-dimensional array of real numbers, size 10*10.
float Matrix[10][10]; // matrix of real numbers, size 10*10
Access to the items of matrix:
// access to the items of the matrix
Matrix[2][4] = -90.3453;
Matrix[0][8] = 259.3;
Matrix[9][9] = 0.85;
Matrix[3][4] = -0.23;
Example zeroing of matrix:
// zeroing of matrix for (i=0; i<10; i++) for (j=0; j<10; j++) Matrix[i][j] = 0.0;
2. Initialization of two-dimensional arrays. Example
Initialization of two-dimensional and multi-dimensional arrays is similar to the initialization of one-dimensional arrays. In this case, a list of initializers any dimension is taken in braces.
Initialization of two-dimensional (multi-dimensional) arrays can be:
- with an indication the array size;
- without indicating the array size (“dimensionless” initialization).
Example 1. Initialization of a two-dimensional array of real numbers M, size 3×4. The size of the array is set.
// initialization of array M of real numbers, size 3*4 float M[3][4] = { { 0.5, -2.8, -1.0, 23.45 }, { -2.3, 0.4, 10.5, 0.8 }, { 12.5, 10.4, 5.4, 3.56 } };
If the list does not include all elements of the group, then the missing elements will be supplemented by zero values automatically (Figure 2).
// initialization of array M of real numbers, size 3*4 // missing items filled with zeros float M[3][4] = { { 0.5, -2.8 }, { -2.3 }, { 12.5, 10.4, 5.4, 3.56 } };
Figure 2. Filling the array with zeros
Example 2. “Dimensionless” initialization of array B of integers.
// "dimensionless" initialization of array B of integers int B[][4] = { { 2, -8, 3, 4 }, { -3, 50, 42, -77 }, { 11, 25, -30, 4 } };
Example 3. “Dimensionless” initializing the array B with filling of zeros (Figure 3).
// "dimensionless" initializing of array B // with filling of zeros int B[][4] = { { 2, -8 }, { -3 }, { 11, 25, -30, 4 } };
Figure 3. “Dimensionless” initializing of array B with filling of zeros
The second array index (column) must be specified. Otherwise, the compiler generates an error message.
3. Initialization of arrays of strings. Examples
An array of strings – a two-dimensional array of characters.
As with other types of data, initialization of the array of rows could be:
- with specifying the size of the string;
- without specifying the size of the string (“dimensionless” initializing).
Example 1. Initialization the array of strings with specifying the length of string.
The two-dimensional array of strings is declared, size 5*60 characters.
char Strings[5][60] = { "Text - 1", "Text - 2", "Text - 3", "Text - 4", "Text - 5" };
In the above example, each string can contain up to 60 characters. If the string length is less than 60, the value of the other characters is 0 (Figure 4).
Figure 4. Presentation of the two-dimensional array of strings in the computer’s memory
Example 2. “Dimensionless” initialization of array of strings
char * Strings2[] = { "String number 1", "String #2", "Third string" };
The above code can be written in another way:
char * Strings2[3] = { "String number 1", "String #2", "Third string" };
You do not need to “manually” calculate the length of the string when “dimensionless” initialization of string array is realized. It is carried out automatically at compile time.
In the above example, the length of first string is 16 characters (see Figure 5). The length of the second string is 10. The length of third string is 12 characters. At the end of each string follows the null character ‘\0’.
Thus, the use of “dimensionless” initialization allows you to save the memory allocated for the array.
Figure 5. Presentation of a string at “dimensionless” initialization
4. Multidimensional arrays. Examples of definition and use
// an array of integers, size 3*5*6 int MI[3][5][6]; MI[0][2][5] = 238; MI[2][4][3] = -3994; // array of reals, size 4*6*2*3 float MF[4][6][2][3]; MF[0][0][0][0] = -394.32f; MF[3][4][1][1] = 4.4f; MF[1][5][0][2] = 0.0f; MF[2][2][1][1] = 555.2437f;
5. Initializing of multidimensional arrays. Example
// initializing of array M of integers, size 2*3*4 int M[2][3][4] = { { { 8, 3, -5, 2 }, { -3, 2, 8, 4 }, { 1, 0, 3, -9 } }, { { -2, 9, 5, 4 }, { 1, 0, 2, 5 }, { -8, 3, 4, 2 } } };
Related topics
- Arrays. Part 1. Array definition. One-dimensional arrays. Initializing array
- Arrays. Part 3. Examples of solving the most common problems with one-dimensional arrays
- Arrays. Part 4. Examples of solving tasks using character strings
- Structures. Part 2. Memory allocation for the structure. Nested structures. Arrays of native structures