Arrays. Part 2. Multidimensional arrays. Stepped arrays

Arrays. Part 2. Multidimensional arrays. Stepped arrays


Content


 1. Which array is called multidimensional?

A multidimensional array is an array that has two or more dimensions. To access an item of a multidimensional array, use a combination of two or more indexes.

 

2. What is the general form of declaring a multidimensional array?

 The general form of declaring a multidimensional array is as follows:

type[,...,] array_name = new type[size1, size2, ..., sizeN];

where

  • type – type of array items;
  • value – the value by which the items of the array are initialized.

 

3. Example of declaring and using a two-dimensional array of real numbers of size 3×4

In the example, a two-dimensional array of real numbers is declared, which are of type float

// declaration and use of an array of real numbers of size 3*4
float[,] M = new float[3, 4]; // allocate memory for the array

// Array filling with arbitrary values
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 4; j++)
        M[i, j] = (float)(i * 0.2 + j * 0.3);

// writing values to individual cells in an array
M[0, 1] = 0.35f;
M[2, 2] = 1.45f;

 

4. Example of declaring and using a three-dimensional array of integers of size 3×4×5
// declaration of a three-dimensional array of integers of size 3 * 4 * 5
ulong[, ,] A; // declare a variable (reference) of the type "three-dimensional array of integers"

A = new ulong[3, 4, 5]; // allocate memory for an array

// write the value to individual cells in the array
A[0, 2, 3] = 232;
A[0, 0, 1] = 300;
A[2, 3, 1] = 20000;

 

5. What does the general form of initializing a multidimensional array look like?

Initializing the array allows you to write values to the cells of the array at the time of its declaration in the program.

The general form of initializing a multidimensional array is as follows:

type[,] array_name = { { value, value, ..., value },
                       { value, value, ..., value },
                       ...
                       { value, value, ..., value } };

where

  • type – type of array items;
  • value – the value by which the items of the array are initialized.

 

6. Example of initialization of a two-dimensional array of size 5×3. Array items are of type char
// initializing a two-dimensional array
char[,] M = { { 'A', 'B', 'C', 'D', 'E' },
              { '8', '-', '=', '>', '+' },
              { '~', 'U', '&', '\\', '@' } };

 

7. Example of initializing a three-dimensional array of integers of size 2×3×4
// initializing a three-dimensional array of integers
short[, ,] B = {
                   {
                       { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       { 9, 10, 11, 12 }
                   },
                   {
                       { 13, 14, 15, 16 },
                       { 17, 18, 19, 20 },
                       { 21, 22, 23, 24 }
                   }
               };

 

8. What is a stepped array? What is the general form of a stepped array declaration?

Two-dimensional arrays can be of two types:

  • rectangular array in the form of a table;
  • stepped array. In this array, the number of items of each row (array) can be different. The stepped array is called an array of arrays.

The general form of a stepped array declaration:

type[][] array_name = new type[size][];

where

  • type – type of array items;
  • size – the number of rows in the array.

 

9. Example of declaring and using a stepped array
// stepped arrays
// declaration of an array of 4 rows whose items are of type ulong
ulong[][] A = new ulong[4][];

// allocating memory for each row
A[0] = new ulong[5]; // there are 5 items in a row with index 0
A[1] = new ulong[3]; // there are 3 items in a row with index 1
A[2] = new ulong[6];
A[3] = new ulong[4];

// demonstrating the access to the items of array A
A[0][0] = 2003;
A[0][4] = 199008;
A[1][2] = 302909;
A[3][3] = 10000000;

for (int i = 0; i < 6; i++)
    A[2][i] = (ulong)(i * i * i);

As you can see from the example, access to the items of the step array occurs differently than in a rectangular array.

 

10. In what cases is it expedient to use stepped arrays?

Stepped arrays are effective in cases, if two-dimensional rectangular array has a lot of items that are not used at all. This allows you to save memory by allocating only the necessary amount of memory (the number of items) for each row of the stepped array. Figure 1 shows the advantage of using step arrays in comparison with rectangular arrays. Figure demonstrates the possible of memory savings in the case where data are presented by rectangular array in which all items of certain rows (0, 2, 3, 4) are not used.

Way 1. Declare a rectangular array named M.

// declaring a rectangular array
int[,] M = new int[5,100]; // 500 cells of type int are allocated

Way 2. Declaring a stepped array.

// declaring a stepped array
int[][] M = new int[5][];

// 50 + 100 + 30 + 60 + 40 = 280 cells of the int type are allocated
M[0] = new int[50];
M[1] = new int[100];
M[2] = new int[30];
M[3] = new int[60];
M[4] = new int[40];

 

Figure 1. An example of saving memory using a stepped array of 5 rows in comparison with a rectangular array of size 5×100

 

11. How to implement an array of two-dimensional arrays? Example

The example implements an array of two-dimensional arrays named M. The array has 10 rows. In each row, a two-dimensional array with a size of 3×4 is formed.

// declaration of an array of 10 two-dimensional arrays of real numbers, each of which has size 3 * 4
float[][,] M; // declaration of variable of the type "array of two-dimensional arrays"

// allocating memory for 10 two-dimensional arrays of type float
M = new float[10][,];

// allocating memory for each array of size 3*4
for (int i = 0; i < 10; i++)
    M[i] = new float[3, 4];

// array filling with arbitrary values
for (int i = 0; i < 10; i++)
    for (int j = 0; j < 3; j++)
        for (int k = 0; k < 4; k++)
            M[i][j, k] = i * 10 + j * 3 + k;

// fill in specific cells
M[0][2, 1] = 20.44f;
M[2][0, 2] = 100.08f;

 


Related topics