This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Preface:
We put in front of the linked list, stack, queue, string, are learning about, next we are going to study, array, array used a lot in our programming, because it is the most basic storage methods, it is divided into a one-dimensional array and two-dimensional array, the array and learned before, we also can be viewed as a linear list, because of an array is an ordered sequence, Let’s take a look at arrays and sparse matrices.
Once a day to prevent decadence
An array of 1.
An Array is an ordered sequence of elements. If you name a collection of a finite number of variables of the same type, the name is the array name. The variables that make up an array are called the components of the array, the elements of the array, and sometimes the subscript variables. The numeric numbers used to distinguish the elements of an array are called subscripts. An array is a form in which several elements of the same type are organized in order for ease of processing in programming. These ordered collections of homogeneous data elements are called arrays. In C, a[8] represents the name of an array, [] represents the writing of an array, 8 represents a number, and then starts with 0 subscript and ends with 7 subscript, followed by an ordered sequence.
1.1 Initializing an Array
int Ic[6]; Int Ic[6]={1,2,3,4,5,6}; / / this value sounds clear and initialize an array of char lc [6] = {' 1 ', '2', '3', '4', '5', '6'}; Double lc[]={1.23, 2.6,5.4,1.5,6.4,3.6}; // This no-number initialization automatically sets the number of elements in the array based on the number of elements you haveCopy the code
1.2 Two-dimensional Array
A two-dimensional array is essentially an array of array elements, that is, “array of arrays”. A two-dimensional array is also called a matrix, and a matrix with equal number of rows and columns is called a square matrix. A two-dimensional array is, in essence, a list of one-dimensional arrays. Declare a two-dimensional array of integers with rows X and columns Y. (A [5] [4],a represents the array name, 5 represents the number of rows, and 4 represents the number of columns)
int a[3][4]; Int a[3][4] = {// Declare and initialize the two-dimensional array {0, 1, 2, 3}, /* initialize the row with index 0 */ {4, 5, 6, 7}, /* Initialize the row with index 1 */ {8, 9, 10, 11} /* Initializes line 2 */}; Int a [3] [4] =,1,2,3,4,5,6,7,8,9,10,11 {0}; / / this can also be int a [], [4] =,1,2,3,4,5,6,7,8,9,10,11 {0}; // There is another way to omit the line numbers, but never omit themCopy the code
1.3 Compressed storage of special matrices
Now that we’ve mentioned it, let’s talk about it briefly, don’t be intimidated by the name, it’s not too difficult, bloggers are a little bit reluctant to talk about it, so let’s talk about it briefly.
1.3.1 Symmetric matrix
Symmetric Matrices refer to Matrices with the principal diagonal as the axis of symmetry and corresponding elements equal. In linear algebra, a symmetric matrix is a square matrix whose transpose is equal to itself.
Calculation formula:
When I <j, j*(j+1)/2+ I is the subscript of the one-dimensional array using the above formula, for example: When the row number is I, the column number is J, and when the value of IC [3][1] is 45 in 2d, the value obtained by the formula is 3*(3+1)/2+1=7, and the value of the one-dimensional array subscript 7 is 45Copy the code
1.3.2 Triangular matrix
Triangular matrix is a kind of square matrix, so named because its non-zero coefficients are arranged in triangular shape. Triangular matrix is divided into upper triangular matrix and lower triangular matrix. The coefficients of the lower left diagonal of the upper triangular matrix are all zero, and the coefficients of the upper right diagonal of the lower triangular matrix are all zero. The triangular matrix can be regarded as a simplified case of the general square matrix
There are two formulas for compressing duplicate values, if your duplicate values are in the upper triangle but you have more rows than columns, you still use the upper triangle formula, but the scope is changed, change the scope.
Upper triangular matrix: when I < = j, I * (2 * n - I + 1) / 2 + j - I when I > j, n * (n + 1) / 2 triangle matrix: when I > = j, I * (I + 1) / 2 + j when I < j, n * (n + 1) / 2Copy the code
1.3.3 Diagonal Matrix
A diagonal matrix is a matrix where all elements except the main diagonal are 0. This matrix is often written as diag (a1, A2… , an). Diagonal matrix can be considered to be the simplest kind of matrix. It is worth mentioning that: the diagonal elements can be 0 or other values, and the diagonal matrix with equal elements on the diagonal is called quantitative matrix; The diagonal matrix with all ones on the diagonal is called the identity matrix. The operation of diagonal matrix includes sum operation, difference operation, number multiplication operation and product operation of diagonal matrix of the same order, and the result is still diagonal matrix.
I don’t want to push the formula, it’s easy, high school math knowledge. It’s almost 2 times I plus j
Conclusion:
Array this article is relatively simple, the blogger did not do too much introduction, because it is not very difficult, interested in the compression matrix can try to improve, a deeper level of research, the blogger is unskilled, just a simple introduction, ha ha ha, pass is not easy, like attention, comments oh!!
The last:# Data structure upgrade learning, chain (chain)