With the foundation of one-dimensional array, let's learn two-dimensional array!!
One dimensional array connection: Detailed explanation of "one-dimensional array" in the review of upgraded computer science (first draft) aquan's blog - CSDN blog
First, the general form of two-dimensional array is
int a[length1][length2]; Array type = array name [subscript 1] [subscript 2];
We can think of a two-dimensional array as an Excel table with rows and columns
length1 represents the number of rows and length2 represents the number of columns
To locate an element in a two-dimensional array, you must specify both rows and columns.
For example, int a[3][4]// Define a two-dimensional array with 3 rows and 4 columns, with a total of 3 * 4 = 12 elements
They are:
a[0][0], a[0][1], a[0][2], a[0][3]
a[1][0], a[1][1], a[1][2], a[1][3]
a[2][0], a[2][1], a[2][2], a[2][3]
If you want to represent the elements in Row 2 and column 1, you should write a[2][1].
//The two-dimensional array can also be regarded as a coordinate system with x-axis and y-axis,
To determine a point in a plane, you must know both the x-axis and the y-axis.
Two dimensional array is two-dimensional in concept, but it is stored continuously in memory;
In other words, the elements of the two-dimensional array are next to each other, and there is no gap between them.
So, how to store two-dimensional arrays in linear memory? There are two ways:
1. One is to arrange by line, that is, put the second line after putting one line;
2. The other is arranged by column, that is, after putting one column, put it in the second column.
However, in "C language", two-dimensional arrays are arranged "by line", that is, first store a[0] line, then a[1] line, and finally a[2] line;
The four elements in each row are also stored in turn. Array a is of type int, each element occupies 4 bytes, and the whole array occupies 4 bytes in total × (3 × 4) = 48 bytes.
You can think that a two-dimensional array is composed of multiple one-dimensional arrays with the same length.
For example, it is easier to understand!
//There are five people in a study group. Each person has the examination results of three courses. Calculate the average score and total average score of each subject of the group.
Name # mathematics # C language # English
Kang Kang 88 59 50
Xiao Wang , 99 , 88 , 77
Lao Wang , 88 , 99 , 66
Zhang San # 87 # 65 # 99
Li Si , 99 , 99 , 99
For this topic, you can define a two-dimensional array a[5][3] to store the scores of 5 people and 3 courses, / / because our C language is executed by line, 5 people and 5 lines
Define a one dimension group v[3] to store the average score of each department,
Then define a variable, average, to store the total average score.
The final programming is as follows:
#include<stdio.h> int main() { int i,j;//Defines the subscript of a two-dimensional array int sum=0;//Initialize the subject score to ensure that the initial score is 0 int average; //Total average score int v[3];//Average score of each subject int a[5][3];//Define a two-dimensional array used to save the scores of each student in each subject printf("Please enter the grades of each subject separately:\n"); //As mentioned in the previous for loop, you must master it. You can only play 666 when you get to the array. Now it comes for(i=0;i<3;i++) { //Let's answer here. Because each student has 3 grades, the outer circle is set as "subject" for(j=0;j<5;j++) { scanf("%d",&a[j][i]);//Enter each student's grades in each subject and remember & don't miss it sum += a[j][i]; } v[i]=sum/5; //Average score of current account sum=0;//Initialize the total score to ensure that the initial test score of the next person is 0 } average = (v[0]+v[1]+v[2])/3; printf(" mathematics:%d \nC language:%d \n English:%d \n",v[0],v[1],v[2]); printf("Toatl:%d\n",average); return 0; }
Initialization (assignment) of two-dimensional array
There are two kinds
1. Assign values according to the line "segmentation"
eg:
int a[2][3]={{1,2,3},{8,9,8}};
2. Assign values by line "continuous"
int a[2][3]={1,2,3,8,9,8};
The results of these two initial values are exactly the same.
Let's take another example here. It is still to calculate the average score and total average score of each subject, but this example requires that the score should be given directly at the time of initialization
#include <stdio.h> int main(){ int i, j; //Two dimensional array subscript int sum = 0; //Total score of current subject int average; //Total average score int v[3]; //Average score of each subject int a[5][3] = {{80,75,92}, {61,65,71}, {59,63,70}, {85,87,90}, {76,77,85}}; for(i=0; i<3; i++){ for(j=0; j<5; j++){ sum += a[j][i]; //Calculate the total score of the current subject } v[i] = sum / 5; // Average score of current account sum = 0; } average = (v[0] + v[1] + v[2]) / 3; printf(" mathematics:%d \nC language:%d \n English:%d \n", v[0], v[1], v[2]); printf("Total: %d\n", average); return 0; }
The following points should be paid attention to when initializing a two-dimensional array:
1. You can only assign values to some elements, and the unassigned elements will automatically take the value of "0"
For example:
int a[3][3] = {{1},{2},{3}};
The first column element of each row is assigned a value, and the value of the unassigned element is 0.
The details are as follows:
a[0][0]=1 a[0][1]=0 a[0][2]=0
a[1][0]=2 a[1][1]=0 a[1][2]=0
a[2][0]=3 a[2][1]=0 a[2][2]=0
2. If all elements are assigned, the "subscript / length of one bit prime group" may not be given, but the two-dimensional array must be given
For example:
int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
It can be written as:
int a[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
3. Two dimensional arrays can be regarded as nested by multiple one-dimensional arrays;
If each element of an array is another array, it is a two-dimensional array. Of course, the premise is that each element must be of the same type.
A two-dimensional array can also be decomposed into multiple one-dimensional arrays.
For example, a two-dimensional array a[3][4] can be decomposed into three one-dimensional arrays, whose array names are a[0], a[1] and a[2].