1. Learning Objectives

  1. Learn how to use two-dimensional arrays
  2. Learn how to use loop nesting

directory

Is C really hard? If you don’t see this picture, you can easily learn C by breaking it into parts. The first article: (one) from the learning error The second: (2) not be so hard to do a simple C language development you understand the process Third: (3) easily understand the first C language program Article 4 (four) C language the basic data types and variables of article 5 (five) C language variables, constants, and operating Article 6: (10) The basic array is not so simple. (10) The pointer of C language is originally like this. (11) THE C language custom function is really very simple. (12) the original structure is such a thing :(13) socket server prepared

C language novice 100 error solutions

Welcome everyone to pay attention to the public account, the public account every 1024 and 1024 times will be lucky draw a mechanical keyboard + 2 IT books oh ~

2. Understand the use of two-dimensional array in C language

In the last chapter we learned about one-bit arrays in C, and we learned that arrays are collections of values of the same type, but in this video we learned about two-dimensional arrays in C.

A one-dimensional array can be thought of as a row of data, and I’m using queuing as an example, where there’s only one row, no columns we can think of it as a one-dimensional array; This time the team is a line, only the horizontal row of students, such as the first named Xiao Ming, the second named Xiao Huang, the third named xiao Green a total of ten students.



In an array, count from 0, the first is the name of the array with a subscript 0, for example, group A is written as a[0] for the first xiaoming, and a[1] for the yellow. How do you represent an array with two rows? That’s where you need a two-dimensional array. Again, using the queue as an example, now the queue has two rows of ten people each. How can the first person in the second row be represented in an array? Since 0 is used to represent the first element in the array, the first person in the second row can be represented as[1] [0], one [] represents a dimension, and two [] represents a two-dimensional array. So again, row 2 in the array, in terms of the number of rows it’s going to be from 0 to 1 and 0 is the first row and 1 is the second row, so the first index is 1; Since it is the first element in the second row, the element 0 of the array is the first element, so the first element in the second row can be represented as[1] [0], assuming the array name is a, then it can be written asa[1][0];

2.1 understanding the usage method of two-dimensional array in C language now there is a number set {1,2,3,4,5,6,7,8,9,10} which needs to be represented by two-dimensional array in C language can be written as follows:

int a[2] [5] = {1.2.3.4.5.6.7.8.9.10};
Copy the code

In the above code, A [2][5] indicates that there are two rows (two lines) of data with five elements in each line. The subsequent data {1,2,3,4,5,6,7,8,9,10} will be stored in two rows. The first row is 1,2,3,4,5, and the second row is 6,7,8,9,10. The third data in the second row can be written as a[1][2], and the second data in the first row can be written as a[0][1].

Try to get the value of the array using the following code:

#include<stdio.h>
void main(a) {
    int a[2] [5] = {1.2.3.4.5.6.7.8.9.10};
    printf("A [0][1] is %d\n",a[0] [1]);
    printf("A [0][2] is %d\n",a[0] [2]);
    printf("A [1][0] is %d\n",a[1] [0]);
}
Copy the code

The results are as follows:



A two-dimensional array can also change its value as follows:

#include<stdio.h>
void main(a) {
    int a[2] [5] = {1.2.3.4.5.6.7.8.9.10};
    printf("A [0][1] is %d\n",a[0] [1]);
    a[0] [1] =1111; 
	printf("A [0][1] changed value is %d\n",a[0] [1]);
}
Copy the code

The running results are as follows:

Understand the use of nested loops and two-dimensional arrays

Nested loops refer to loops that have code within them, such as a for loop that also has a for loop within it.

Suppose two for loops are nested. The condition for the external for loop to jump out is I <5, the initial value of I is 0, and the external for loop will loop five times. The inner for loop has a loop condition of j<5 and an initial value of j 0. So when the outer loop loops once the inner for loop loops five times; The external loop needs to jump out when the conditions are not valid, and the loop will continue to jump out if the loop is not satisfied obviously for 1 time. This creates an external for loop once and an inner loop five times. Let’s use the example to see, the demo code is as follows:

#include<stdio.h>
void main(a)
{
    int i,j; 
    for(i=0; i<5; i++) {for(j=0; j<5; j++) {printf("The value of I for the outer loop is %d and the value of j for the inner loop is %d\n",i,j); 
        }
		printf("\n"); }}Copy the code

The above code defines two variables, one is I and the other is j. I and j are initialized in the for loop, I =0; With j = 0; This code executes only once during the entire loop and will be executed again unless the loop is restarted from the beginning. The external for loop has the same condition as the internal for loop, the loop variable is less than 5, but the loop variable is different, because the loop variable increases during the loop, because the loop is nested, the external loop is executed once, then the internal loop will execute 5 times.

The running results are as follows:



3.2 Understand the combined use of circular nesting and two-dimensional arrays

In the previous section, we learned that loop nesting causes the outer loop to execute once and the inner loop to execute multiple times. With this in mind, we can use loop nesting to get all the values of a two-dimensional array.

Suppose there is a two-dimensional array with 2 rows and 5 elements in each row. The elements in the first row are [0][1],[0][2],[0][3],[0][4],[0][5], and we can obviously find that the first dimension subscript does not change, but the second subscript is incresed one by one. Then you can use loop nesting to get all the values as follows:

#include<stdio.h>
void main(a)
{
    int a[2] [5] = {1.2.3.4.5.6.7.8.9.10};
    int i,j;
    for(i=0; i<2; i++) {for(j=0; j<5; j++) {printf("Array [%d][%d] is %d\n",i,j,a[i][j]); }}}Copy the code

In the code above, the contents of the outer loop are I less than 2 and increment each time, and the j of the inner loop is less than 5 and increment each time, so the outer loop executes once and the inner loop executes five times. When I of the outer loop is 0, the first time of the inner loop is a[0][0] and the second time is a[0][1]. We take the variables I and j as the subscripts of the array A, and then all the two-dimensional array values will be obtained.

The results are as follows:

Four,

Through the above description and explanation, we know the following contents:

  1. We know what a two-dimensional array is
  2. Learn how to use loop nesting
  3. Learn about using circular nesting to get values from two-dimensional arrays