C language array operation and array supplementary knowledge, and some knowledge supplement

Foreword: I've been learning the basis of java background development (judgment, loop,), so this week's C language only ...

Foreword: I've been learning the basis of java background development (judgment, loop,), so this week's C language only learned some supplementary knowledge of array operation. Generally speaking, this blog is more inclined to transition. After finishing java in two days, I will write another article on the basics of java.

1, 2D array:

1. Definition format of two-dimensional array: type name array name [row length] [column length];

    Example: int a[3][5];

    It is generally understood that a is a matrix with three rows and five columns.

2. Initialization of two-dimensional array: there are two initialization methods for two-dimensional array.

(1) . initial value assigned by branch

        The general form is: type name, array name [row length] [column length] = ,..., ,...};

        It means to assign the data in the initial value table k to the elements in row k in turn. For example:
        int a[3][3]={ , , };       At this time, the elements in the a array are:

123456789

        The initialization of a two-dimensional array can also be only for some elements, such as:

int b[4][3]={ , {} , };

         This assignment only assigns initial values to all elements in row 0 of array b and the first two elements in Row 2, and the initial values of other elements are 0.

  (2) . assign initial value in sequence:

        The general form is: type name array name [row length] [column length] = ;

        Assigning an initial value in this way will assign the data in the initial value table to the array elements in turn according to the storage order of the array elements in memory             Su. For example, int a[3][3]=; Equivalent to int a [3] [3] = , , ;

        If you want to use the sequential assignment method to assign values to some elements in the array, you need to write 0 as well, because you should pay attention to           The writing order of data in the initial value table.

In general, the branch assignment method is more intuitive and clear, and it is more recommended to assign values to two-dimensional arrays with branch assignment.

Note: when initializing a two-dimensional array, if all elements are assigned initial values, or if all rows are listed in the initial value table when assigning initial values to branches, the row length can be omitted, but it is not recommended.

3. Traversal of two-dimensional array:

The traversal of a two-dimensional array requires two loops. The first loop is used to traverse the row length and the second loop is used to traverse the column length to traverse a matrix or two-dimensional table. As follows:

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

2, Use the array for faster prime judgment:

#include<stdio.h> /* To determine whether a number is a prime number It can be determined whether this number can be divided by a known prime number less than x Therefore, we first need a prime table, and use this prime table to construct the next prime. */ int isprime(int i,int prime[],int length); int main() { const int number=100;//Build an array to store the first 100 primes. int prime[number]=; /* The first term of the array is 2, because 2 is the first prime we know Next, it is judged that the cycle of prime numbers starts from 3. As long as i in the following cycle cannot be divided by the prime number in front of it, the number is also a prime number and will be assigned to the corresponding position in the prime array. Therefore, the prime array is constantly assigned a value, and the condition for judging whether the latter number is a prime number is whether the number can be divided by the existing prime number in the prime array, and so on. Therefore, this program is to judge whether it is a prime number and construct a prime number table at the same time. */ int cnt=1;//Here cnt starts counting from 1 because there is already a 2 in the prime array. int i=3;//When there are already 2, the cycle judgment starts from 3. while(cnt<number){ if(isprime(i,prime,cnt)){ prime[cnt++]=i; /*Two things are done in this step. The first thing is to put the judged prime i in the corresponding position of the array, The second thing is to use cnt + + to put the value of prime i in the latter position next time The value obtained by cnt + + is the value before cnt plus 1, so this feature can be used for assignment. */ } i++; } for(i=0;i<number;i++){ printf("%d",prime[i]); if((i+1)%5){ printf("\t"); }else{ printf("\n"); } } return 0; } int isprime(int x,int prime[],int length) { int ret=1; int i; for(i=0;i<length;i++){ if(x%prime[i]==0){ ret=0; break; } } return ret; }

3, Search:

1. Search in programming represents the position where a number needs to be found in an array (or confirm whether the number exists in the array). The basic method of search is to traverse the array and compare each element in the array with the number one by one. Linear search is a more basic search method.

2. Binary search:

Linear search has a large linear problem. Sometimes it is necessary to complete the loop to judge whether a number exists in the array. It is inefficient and slow. Therefore, there is binary search. Personally, I think its principle is like the binary method used to find the zero point in mathematics.

When there is an array in which each element is arranged in a certain order, such as from small to large or from large to small, binary search can be used. First, set the number at the first position of the array to left, the number at the rightmost position to right, and the element in the middle to Mid. the value of mid is equal to the addition of left and right divided by two, and compare the data corresponding to the mid index with a certain number, If it is greater than a certain number, assign the value of mid-1 to right, then reconstruct and calculate mid, and compare again until finally find the position of the number in the array or determine that the number is not in the array.

#include<stdio.h> int search(int key,int a[],int length) { int ret=-1; int left=0; int right=length-1; int mid; while(right>left){ mid=(left+right)/2; if(a[mid]==key){ ret=mid; break; }else if(a[mid]>key){ right=mid-1; }else{ left=mid+1; } } return ret; } int main() { int x; int a[]=; scanf("%d",&x); int y; y=search(x,a,sizeof(a)/sizeof(a[0])); if(y!=-1){ printf("%d In the first row of the array%d In two places",x,y); }else{ printf("%d Not in array",x); } return 0; }

4, Application of sizeof:

1.sizeof() function can be used to obtain the length of an array, so we don't need to calculate the length of an array again after modifying it in programming.

2. The array length obtained by sizeof is the byte length of the array, and the length of a cell in the array is 4.

If there is an array a []; Then sizeof(a) is the byte length of the whole array, and sizeof(a[0]) is the byte length of a unit in the array. Then, when we need to get the number of elements in an array, we can use sizeof(a)/sizeof(a[0]) to represent the number of elements in the array.

#include<stdio.h> int numinlist(int x,int a[],int length); int main() { int a[]=; int x; int y=0; scanf("%d",&x); // printf("%lu\n",sizeof(a)); // printf("%lu\n",sizeof(a[0])); // printf("%lu\n",sizeof(a)/sizeof(a[0])); // for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){ // printf("%d\n",a[i]); // } // for(int j=0;j<sizeof(a)/sizeof(a[0]);j++){ // if(x==a[j]){ // printf("%d at position% d of array", x,j); // break; // } // } y=numinlist(x,a,sizeof(a)/sizeof(a[0])); if(y==1){ printf("In array"); }else{ printf("Not in array"); } return 0; } int numinlist(int x,int a[],int length){ int ret=0; for(int i=0;i<length;i++){ //When an array is used as a parameter in a function, it is often necessary to use another parameter to represent the length of the array. sizeof cannot be used to calculate the length of the array in a function if(x==a[i]){ ret=1; break; } } return ret; }

Note: when an array is used as a variable in a function, we often need to pass another variable to represent the number of cells in the array at the same time, instead of directly using sizeof to find the number of cells in the array inside the function.

7 November 2021, 14:04 | Views: 1964

Add new comment

For adding a comment, please log in
or create account

0 comments