When processing the data in a two-dimensional array, each element has two subscripts.

It is feasible to process two-dimensional table with one-dimensional array, but it will be very complicated, especially when it comes to the input, processing and output of two-dimensional table.

When you are racking your brains, the two-dimensional array (the big brother of one-dimensional array) appears in front of you like the hero of saving beauty in TV series. What are the friends who have just met the array waiting for? Let’s meet the elder brother with excellent martial arts!

1. Double subscript variable

Smart, you must be able to just as the name suggests, take a simple example: array [2] [3]

Array is the name of the array, and the following two square brackets are placed separatelyLine subscriptandColumn subscriptThe row and column subscripts here are calledDouble subscript

The subscript rules as like as two peas of one dimension array are the same.

  1) Can be a positive shaping variable:

    eg:Array [2] [3];

  (2) Can be a character constant:

Eg: array [‘a ‘] [‘b’] = array [65] [66]; (unclear)character constants You can click:

https://baike.baidu.com/item/%E5%AD%97%E7%AC%A6%E5%B8%B8%E6%95%B0?fromtitle=%E5%AD%97%E7%AC%A6%E5%B8%B8%E9%87%8F&fromid=103245)

  (3) It can be an expression with definite value:

    eg:Array [i+1] [j+2]、Array [ A[1] ] [ A[2] ];

All in all, the subscripts here denoteNumber of rowsandNumber of columns

Next, feel the power of double subscript variables with Xiaobian.

Superscript and subscript variables can make the calculation of equations easier, for example:

Calculate a linear system of equations of degree two variables

$$\left\{\begin{matrix}7*x_{1}-4*x_{2}=7\\-2*x_{1}+3*x_{2}=-1\end{matrix}\right.$$

Its general expression is as follows:

$$\left\{\begin{matrix}a11*x1+a12*x2=b1\\a21*x1+a22*x2=b2\end{matrix}\right.$$

We can write an augmented matrix of the equations

$$\begin{pmatrix}a11& a12& b1\\a21& a22& b2\end{pmatrix}$$

Using Cramer’s law, we can get the following conclusions

$$x1=(b1*a22-b2*a12)/(a11*a22-a21*a12)$$

$$x2=(a11*b2-a21*b1)/(a11*a22-a21*a12)$$

Because the essence of matrix is actually a two-dimensional number table, we can use two-dimensional array to represent the coefficients of the equations.

First, we define a two-dimensional table: a [2] [2], and a one dimensional table: B [2]

So a11 can be written as: a [0] [0], A12 can be written as: a [0] [1], A21, A22. Then:

$$x1=(b[0]*a[1][1]-b[1]*a[0][1])/(a[0][0]*a[1][1]-a[1][0]*a[0][1])$$

X2 is the same.

Therefore, through double subscript variables, we only need to assign values to the elements in a [i] [J] and B [i], and then we can easily solve the problem of equations.

In addition,A two-dimensional table is represented by double subscript variables, so that the row and column subscripts of subscript variables correspond to the position of data in the table, which visually reflects the two-dimensional table.

2. Two dimensional array definition

Very simple, an array defined by a double subscript variable is called a two-dimensional array, and the double subscript variable is the element of the array.

The general form of the definition of two-dimensional array is similar to that of one-dimensional array, except that there is one more constant expression (dimension plus one) in the following table:

  [] []

For example: float a [3] [4], B [5] [6];

The float here indicates that the element type in the two-dimensional array is single precision floating-point type, and a and B are the names given to the array.

  Note: a [3] [4] don’t write a [3, 4]!

In the above, we understand two-dimensional arrays asTwo dimensional chartWe can also think of two-dimensional arrays asElements are one-dimensional arrays of one-dimensional arraysTo reduce the dimension.

For example, “a [3] [4], we can think of a as a one-dimensional array, in which there are three elements: a [0], a [1], a [2], and each element contains four elements:

a [0]           a [0] [1] a [0] [2] a [0] [3] a [0] [4]
a [1] a [1] [1] a [1] [2] a [1] [3] a [1] [4]
a [2] a [2] [1] a [2] [2] a [2] [3] a [2] [4]


This method is very convenient for data initialization and pointer representation.

In C language, the order of elements in two-dimensional array isDeposit by lineThat is to say, first arrange the data in the first row, then arrange the data in the second row, and so on. As shown in the figure:

When processing the data in a two-dimensional array, each element has two subscripts.

Array elements can appear in expressions or be assigned values, for example: B [1] [2] = a [1] [3] / 2.

  I want to remind you that we should strictly distinguish between a [5] [6] when defining arrays and a [5] [6] when referring to elements!

3. Initialization of 2D array

There are several ways to initialize a two-dimensional array

(1) Assign a value to a two-dimensional array

This method uses the two-dimensional array we talked about earlierDimension reduction understandingFor example:

int a [2] [3] = {{1, 2, 3}, {4, 5, 6}};

In this sentence, the first line elements are assigned to 1, 2 and 3 in turn, and then the second line elements are assigned to 4, 5 and 6, so it is assigned by line.

(2) There can be only one curly bracket to assign values to each element in the order of the elements of the two-dimensional array

  For example:

int a [2] [3] = {1, 2, 3, 4, 5, 6};

(3) Assign initial values to some elements

  ①

int a [2] [3] = {{2}, {4}};

This statement means that only the first element of the first line is assigned a value of 2, the first element of the second line is assigned a value of 4, and all other elements are assigned a value of 0.

  ②

int a [2] [3] = {{1, 2, 3}};

This statement means that only three elements in the first line are assigned values of 1, 2 and 3 respectively.

  ③

int a [2] [3] = {{},{1, 2, 3}};

This statement means that only three elements in the second line are assigned values of 1, 2 and 3 respectively.

4) When defining an array, the length of the first dimension may not be defined,But the length of the second dimension must be defined

  Example:

int a [] [3] = {1, 2, 3, 4, 5, 6};

The system will allocate storage space according to the total number of input, so it is easy to know that the two-dimensional array has two rows;

Another example is:

When processing the data in a two-dimensional array, each element has two subscripts.

That’s all about the knowledge of two-dimensional array. Welcome to communicate!

Preview: application of two dimensional array

  2020-04-21 

  17:26:54