## 1. Multidimensional array

### (1) Definition of array

The array is a limited sequence composed of n (n≥1)`相同数据类型` data elements. When defining the array, a fixed size memory space will be allocated for the array. To store elements, the dimensions of an array cannot be changed after it is defined.

After the array's dimensions and dimension boundaries are determined, the number of elements is fixed, so insertion and deletion operations cannot be performed. The two most common operations on arrays are search and modification.

### (2) Two-dimensional array

Arrays can be divided into one-dimensional arrays and multi-dimensional arrays (two-dimensional arrays are common). Two-dimensional arrays can be regarded as one-dimensional arrays of one-dimensional arrays. The sequence table is a one-dimensional array, so it is `线性结构`, which is the same logical structure as the stack, queue, and string. The multi-dimensional array is a typical non-linear structure, and it can also be said to be nested. linear structure.

For example, a two-dimensional array A[3][4] is actually a one-dimensional array of length 3 in memory, and each element is a one-dimensional array of length 4, which corresponds to three rows and four columns. It is stored from top to bottom and from left to right, as follows:

0 1 2 3
0 [0，0] [0，1] [0，2] [0，3]
1 [1，0] [1，1] [1，2] [1，3]
2 [2，0] [2，1] [2，2] [2，3]

Since the array starts from subscript 0, in a two-dimensional array with m rows and n columns, the first element is [0, 0] and the last element is [m-1, n-1]. The above three The last element in the two-dimensional array A[3][4] with four rows and four columns is [2, 3].

### (3) Storage of multi-dimensional arrays

The storage of two-dimensional array is different from that of one-dimensional array. There are two storage methods, which can be divided into `行优先存储` and `列优先存储`. The former is to press After each row is full, it continues to the next row. On the contrary, the latter first stores each column until it is full before continuing to the next column.

For example, define a two-dimensional array A[3][3] in a continuous memory space as follows:

If stored in row priority, taking A[2][0] as an example, before storing A[2][0], it is stored like this:

And in column order Priority storage, taking A[1][1] as an example, before storing A[1][1], it is stored like this:

### (4) Related calculations of subscripts of multi-dimensional arrays

Assume a two-dimensional array A[i][j], in which the ranges of row subscripts and column subscripts are [0, a] and [0, b] respectively. If each array element occupies L storage units in memory, And the storage location of the first element in the array is LOC[c1][c2]. Find the storage location of any element A[i][j] in the two-dimensional array?

1. Store rows first
[`所求行乘列界限加1，然后加所求列确定位置`]
(1) First determine how many rows there are, and add The above number is then multiplied by the storage unit, and finally the storage location of the first element is added to get: LOC[i][j]=LOC[c1][c2]+[(i-c1)×(b-c2 +1)+(j-c2)]×L.
(2) In a programming language, since the subscripts of array elements start from 0, the formula is rewritten as: LOC[i][j]=LOC[0][0] +[i×(b+1)+j]×L.

For example, the two-dimensional array A[m][n] is stored in row-major mode, and each element occupies L storage units. The storage address of element A[0][0] is b. Find the storage address of element A[i][j] (0 ≤ i ≤ m-1, 0 ≤ j ≤ n-1).

Analysis: Since the row and column elements in the two-dimensional array all start from 0, that is, LOC[i][j]=b+[i×(n-1+1)+j]×L=b+[i ×n+j]×L.
2. Column-first storage
[`所求列乘行界限加1，然后加所求行确定位置`]
(1) First make sure there is How many columns, add the number of rows, then multiply by the storage unit, and finally add the storage location of the first element, we get: LOC[i][j]=LOC[c1][c2]+[(j-c1)×( a-c2+1)+(j-c1)]×L.
(2) In a programming language, since the subscripts of array elements start from 0, the formula is rewritten as: LOC[i][j]=LOC[0][0] +[j×(a+1)+i]×L.

For example, assume that the array a with 7 rows and 6 columns is stored in column-major order, the base address is 1024, and each element occupies 2 storage units. If there is no row 0 and column 0, find the value of row 4 and column 5. The storage address of the element.

Analysis: Since the first element is a[1][1], it needs to be subtracted and then substituted into the calculation, that is,
LOC[4][5]=1024+ [(5-1)×(7-1+1)+(4-1)]×2=1024+62=1086.

• For an array An×n (square matrix), its element aij a>The difference in addresses when storing row-first and column-first is (n-1)(i-j).

## 2. Matrix

### (1) Special matrices and sparse matrices

A matrix in which the same elements or zero elements are distributed in a certain pattern in the matrix is ​​called `特殊矩阵`, and vice versa is `稀疏矩阵`. To put it simply, since a special matrix is ​​special, it means that there are many identical or zero elements in it, and there are certain rules distributed in the matrix.

Common special matrices include symmetric matrices, antisymmetric matrices, upper/lower triangular matrices, diagonal matrices, etc. For example, in a diagonal matrix, there are only elements on the diagonal, and the remaining elements are zero:

### (2) Symmetric matrix and its compressed storage

If an n-order square matrix satisfies Ai×j=Aj×i If it is compressed and stored, if the elements are stored in a one-dimensional array in row order To implement, first determine the array size. Since the matrix is ​​a 4×4 square matrix with an order of n=4, the required one-dimensional array size is n(1+n)/2=(4×5)/2=10 , because it is a symmetric matrix, only the elements of the upper or lower triangular part are stored. Here, we take the storage of upper triangular elements as an example:
For example, matrix B is as follows, consider it as a symmetric matrix: , it is called a symmetric matrix. Since the elements of the upper triangular part and the lower triangular part of the symmetric matrix correspond to the same, in order to avoid wasting space when storing the symmetric matrix, you can only store the elements of the upper or lower triangular part and store them in a one-dimensional array. The size of is 1+2+…+n=n(1+n)/2.

### (3) Diagonal matrix

A tridiagonal matrix is ​​a diagonal matrix in which non-zero elements are concentrated in the three diagonal areas centered on the main diagonal line, and other areas are zero.

### (4) Compressed storage of sparse matrices

As mentioned earlier, the distribution of non-zero elements in sparse matrices is opposite to that of special matrices and is irregular. Most of the elements in a sparse matrix are 0, and like the distribution of non-zero elements, they are also irregular. The purpose of matrix compression is to save storage space.
1. Triplet table
In order to compress and store a sparse matrix, not only the value of the non-zero element in the matrix must be stored, but also the element must be stored. The row and column where it is located form a triplet table (row, column, value), thereby reducing storage space. Since the non-zero elements in the sparse matrix and their corresponding row and column numbers are stored in an array in the form of triples, it is impossible to directly access the elements of the matrix through the subscripts of the array after such compressed storage. , losing the random access characteristics. In addition, the triple table of sparse matrices can also be stored using the cross linked list method. [The two storage structures of sparse matrices are triple tables (arrays) and cross linked lists]

``````//以整型int为例，可替换其他类型
typedef struct{

int i,j;	//行与列
int x;		//值
}Sparsematrix;
``````

For example, a sparse matrix A is compressed and stored:

The corresponding triple table is as follows:

i (line) j (column) x (value)
1 1 4
1 3 2
2 0 5

For example, there is a 100×90 sparse matrix with 10 non-zero elements. Assume that each integer occupies 2 bytes. When using triples to represent the matrix, find the required number of bytes.

Analysis: The triple table includes rows, columns, and values. Each integer occupies 2 bytes, so 10 non-zero elements occupy 3×2×10=60 bytes. In addition, there are also the number of rows and columns in the triple table. The number and the total number of non-zero elements total 6 bytes, a total of 60+6=66 bytes.