catalogue
Arrays are divided into one-dimensional arrays and two-dimensional arrays.
Let's see the most basic definition and initialization problems
Now let's talk about some other details
2, I don't have much to say. Let's talk about it.
1, Basic two-dimensional array problem
1, What is an array?
Arrays are divided into one-dimensional arrays and two-dimensional arrays.
Whether it is a one-dimensional array or a two-dimensional array, it is an ordered set of the same data type. It can regard a series of data of the same type as a whole, name it with a name, identify the component with a subscript, and store it continuously in memory. The array elements can be uniquely determined with the array name and subscript.
Let's see the most basic definition and initialization problems
1. One dimensional array
Data type array name[Array length]; char str[100]; char str[100] = { 1,2,3 }; char str[100] = { 'a','b','c' }; char str[100] = "abc"; char str[]= { 'a','b','c' };//When all the data is given, the array length can be omitted
Also mentioned:
Why? Why is the output different with such a small difference?
It's no use for me to block this sentence.
This is a basic concept problem. When defining an array, initialization is to operate the entire array, and the latter method is to assign a value to a[10], but the array length is 10 and the maximum subscript is 9, so {} must be added, otherwise an error will be reported.
I don't know the use of curly braces.... There is no eleventh element in the a array.
Then the evil thing comes again.
We can know from the above three pictures
1) The array is uninitialized. The values in the array are random values.
2) The array is initialized to {0}, and the values in the array are 0.
3) The array is initialized to {non-zero value}. The first value in the array is a non-zero value, and other values are 0.
2. Two dimensional array
Data type array name[Row subscript][Column subscript]; char a[100][100]; char a[3][4] = { 1,2,3,4,5,6,7,8,9,0,11,12 }; char a[3][4] = { {1,2,3,4},{5,6,7,8},{9,0,11,12} }; //You can add curly braces or not. They are the same When omitting the length of a two-dimensional array, you can only omit the writing of line subscripts, which is specified
Is there such evil in two-dimensional arrays?
Hey, there are!
Would that be much more convenient? Hey, hey, don't say why I don't talk about some basic things. We've all started to study deeply. I only talk about the important things that are too basic.
This is also a common method of initializing two-dimensional arrays.
Let's take another look at the initialization of string array. Because the string has a special input function gets, which inputs a whole line of string, it can be initialized with only one for loop.
After entering a line each time, enter a enter key to jump out of the current for loop and proceed to the next time. You can write less but not more, and the more part will be discarded.
Now let's talk about some other details
1.printf, scanf and puts, gets
scanf ends when a space or carriage return is encountered
gets will not end until it encounters a carriage return.
scanf("%d",&arr);//Generally, scanf is followed by the & sign. If it is written to the array, it doesn't matter whether it is added or not. sacnf("%s",str); gets(str)//Just use the array name
2. The difference between "a" and "a"
"A" takes two bytes. Because "" is a string flag, the system will automatically add an end identifier '\ 0' to the end of the string, so this takes two bytes
'a' only occupies one byte, because '' is a character flag. Only a has one character and only occupies one byte.
3. The array name is an address constant, which stores the first address of the array memory space. If it is an & array name, it takes out the address of the whole array. Of course, there is no difference between the two values, but there will be a difference under certain circumstances. C language stipulates that only a single array element can be referenced, not the whole array at one time.
The meaning of array name plus 1 is the size of one element backward, and the meaning of array name + 1 is the length of one array backward.
4. The subscript starts from 0 and cannot be out of bounds. Once the subscript goes out of bounds, the data will be written to the storage unit occupied by other variables, or even to the program code segment, which may cause unpredictable consequences.
5. Although C language stipulates that only statically stored arrays can be initialized, general C language compilation systems allow initial values to be assigned to dynamically stored arrays. If statically stored arrays are not initialized, the system will automatically assign 0 to all array elements;
6. When using arrays, macro definitions are generally used.
Macro definition has the advantage of macro definition, which is more convenient to modify. You only need to modify it after define;
7.C language supports multi-dimensional arrays, and the most common multi-dimensional array is two-dimensional array.
2, I don't have much to say. Let's talk about it.
1, Basic two-dimensional array problem
//Number of outputs greater than average #include<stdio.h> int main(){ int i,n; double ave,sum; int a[10]; printf("Please enter n: "); scanf("%d",&n); if(n>=1&&n<=10){ printf("Please enter%d Data:",n); for(i=0;i<n;i++){ scanf("%d",&a[i]);//A loop statement is required to enter the data of the array } sum=0; for(i=0;i<n;i++){ sum+=a[i];//You also need a circular statement to accumulate } ave=sum/n; printf("ave=%.2f\n",ave); printf(">ave"); for(i=0;i<n;i++){//The for loop in the array cannot use the equal sign. If you use the equal sign, you need to output one more data, because the array starts from zero if(a[i]>ave) printf("%d ",a[i]); } printf("\n"); } else printf("Invlid Value.\n"); return 0; }
#include<stdio.h> #define MAXN 46 / / the definition is at the top. If you need to change it, you only need to change this place. Arrays can be so convenient int main(){ int i,n; int fib[MAXN]={1,1};//Output the first two items first printf("Please enter n: "); scanf("%d",&n); if(n>=1&&n<=46){ for(i=2;i<n;i++){//i=2 because the array starts from zero. When i=2, it is actually the third term fib[i]=fib[i-1]+fib[i-2];//The former is equal to the sum of the latter two } for(i=0;i<n;i++){ printf("%6d",fib[i]); if((i+1)%5==0) printf("\n"); } if(n%5!=0) printf("\n"); } else printf("Invalid Value!\n"); return 0; }
#include<stdio.h> #define MAXN 8 int main(){ int i,n,response; int count[MAXN+1];//The actual problem starts with 1, so you can define a longer one. In fact, you can also define a longer one when defining a macro printf("Please enter n: "); scanf("%d",&n); for(i=1;i<=MAXN;i++) count[i]=0;//Initialization of data to avoid the influence of data before the storage unit!!!!!!! for(i=1;i<=n;i++){ printf("Enter your response:"); scanf("%d",&response); if(response>=1&&response<=MAXN) count[response]++;//It is equivalent to count[response]+=1. The above has been initialized, so it will be accumulated once it occurs else printf("Invalid:%d\n",response); } printf("result:\n"); for(i=1;i<=MAXN;i++){ if(count[i]!=0) printf("%4d%4d\n",i,count[i]); } return 0; }
This question needs to see!!!
#include<stdio.h> #define MAXN 6 #define MAXM 6 int main(){ int col,i,j,m,n,row; int a[MAXM][MAXN]; printf("Please enter m,n: "); scanf("%d%d",&m,&n); printf("Please enter%d number:\n",m*n); for(i=0;i<m;i++){ for(j=0;j<n;j++){ scanf("%d",&a[i][j]); } }//Initialization method of two-dimensional array, double for loop row=col=0; for(i=0;i<m;i++){//Traverse the array to find the maximum value for(j=0;j<n;j++){ if(a[i][j]>a[row][col]){ row=i; col=j; } } } printf("max=a[%d][%d]=%d\n",row,col,a[row][col]); return 0; }
If you get familiar with the above things, you can solve the basic one-dimensional and two-dimensional array problem.
2, Deep usage of arrays
Of course, the role of array is more than that. The advantage of array lies in its large number of elements and its selectivity. Because there are many elements, you can choose a variety of elements. This is particularly important later (so let's talk about it later, hey hey).
Here we will mainly talk about the other two main functions, sorting and searching.
Previous title:
1. Sequential search method
#include<stdio.h> #define MAXN 10 int main(){ int i,flag,n,x; int a[MAXN]; printf("please input n,x:"); scanf("%d%d",&n,&x); printf("Please enter%d number:",n); for(i=0;i<n;i++){ scanf("%d",&a[i]);//Adding data to an array } flag=0; for(i=0;i<n;i++){ if(a[i]==x){ printf("Index is %d\n",i); flag=1; } } if(flag==0) printf("Not found!\n"); return 0; }
2.
#include<stdio.h> #define MAXN 10 int main(){ int i,index,n; int a[MAXN]; printf("Enten n:"); scanf("%d",&n); printf("Please enter%d number:",n); for(i=0;i<n;i++) scanf("%d",&a[i]);//Array input data, with the help of a loop. index=0;//Assume that the index (first address) is the subscript of the smallest item for(i=0;i<n;i++){ if(a[i]<a[index]) index=i;//If there is one smaller than him, change to the first address } printf("min is %d\tsub is %d\n",a[index],index); return 0; }
3. Find the same in a and b (focus on question 4)
#include<stdio.h> int main(){ int a[]={1,2,3,4,5},b[]={3,4,5,6,7,8},c[6]; int i,j,k=0; for(i=0;i<5;i++){ for(j=0;j<6;j++){ if(a[i]==b[j]){ c[k]=a[i]; k++; break; } } } for(i=0;i<k;i++){ printf("%d ",c[i]); } return 0; }
4. Find the difference between a and b
#include<stdio.h> #define N 10 int main(){ int m,n,i,j,k,a[N],b[N],c[2*N],flag=0; printf("Please enter a number:"); scanf("%d",&m); printf("Please enter%d Data:",m); for(i=0;i<m;i++){ scanf("%d",&a[i]);//Initialize a array } printf("Please enter a number:"); scanf("%d",&n); printf("Please enter%d Data:",n); for(i=0;i<n;i++){ scanf("%d",&b[i]);//Initialize b array } for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(a[i]==b[j]) break;//Take out the elements in array a and compare them with those in array b } if(j>=n){ flag=1; c[k++]=a[i];//If the normal cycle ends, it indicates that the element in a has no same element as that in b, and flag=1 } } for(j=0;j<n;j++){ for(i=0;i<m;i++){ if(b[j]==a[i]) break; } if(i>=m){ flag=1; c[k++]=b[j];//b should also be compared with a } } for(i=0;i<k;i++) printf("%d ",c[i]); if(flag==0) printf("No found!");//Avoid two arrays without non-public elements and no operations return 0; }
5. Maximum and minimum exchange position
#include<stdio.h> #define N 10 int main(){ int n,i,t,a[N],max,min; printf("Please enter a number:"); scanf("%d",&n); printf("Please enter%d number:",n); for(i=0;i<n;i++) scanf("%d",&a[i]); max=0; min=0;//Initialize the max min value so that the first item is the max min value. for(i=0;i<n;i++){ if(a[max]<a[i]) max=i; if(a[min]>a[i]) min=i; } t=a[max]; a[max]=a[n-1]; a[n-1]=t; //The assignment process must be placed outside the for loop. The maximum and minimum can be known only after the loop is completed. Remember t=a[min]; a[min]=a[0]; a[0]=t; printf("%d %d",a[0],a[n-1]); return 0; }
6. Sorting by selection method
#include<stdio.h> #define MAXN 10 int main(){ int i,index,k,n,temp; int a[MAXN]; printf("Please enter n: "); scanf("%d",&n); printf("Please enter%d number:",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(k=0;k<n-1;k++){ index=k; for(i=k+1;i<n;i++){ if(a[i]<a[index]) index=i; } temp=a[index]; a[index]=a[k]; a[k]=temp; }//The maximum K is only n-2, and the maximum I is n-1, that is, a[k] is compared with a[i]. After the for loop ends, the subscripts have been exchanged, and then the data is exchanged printf("Sort as:"); for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n"); return 0; } //In the first round, the smallest record is selected from the records to be sorted r[1]-r[n], and it is exchanged with r[1]; In the second round, the smallest record is selected from the records to be sorted r[2]-r[n], and it is exchanged with r[2]; By analogy, in the ith pass, the smallest record is selected from the records to be sorted r[i]~r[n], and it is exchanged with r[i], so that the ordered sequence continues to grow until all sorting is completed.
7. Binary search method
#include<stdio.h> int main(){ int low,high,mid,n=10,x; int a[10]={1,2,3,4,5,6,7,8,9,10}; printf("Please enter x: "); scanf("%d",&x); low=0; high=n-1; while(low<=high){ mid=(high+low)/2; if(x==a[mid]) break; else if(x<a[mid]) high=mid-1; else low=mid+1; }//This loop is the main part of the binary search method. if(low<=high) printf("Index is %d\n",mid); else printf("Noy Found"); return 0; }
8. Bubble sorting
#include<stdio.h> #define MAXN 10 void swap(int *px,int *py); void bubble(int a[],int n); int main(){ int n,a[MAXN],i; printf("Enter n(n<=10):"); scanf("%d",&n); printf("Enter %d integers:",n); for(i=0;i<n;i++) scanf("&d",&a[i]); bubble(a,n);//Just use the array name. printf("After sorted:"); for(i=0;i<n;i++) printf("%3d",a[i]); return 0; } void bubble(int a[],int n){ int i,j,t; for(i=1;i<n;i++){ for(j=0;j<n-i;j++){ if(a[j]>a[j+1]) swap(&a[j],&a[j+1]); } } } void swap(int *px,int *py){ int t; t=*px; *px=*py; *py=t; }
***9. There are three methods to judge palindrome array recursion pointer
1) Array
#include<stdio.h> #define MAXLINE 80 int main(){ int i,k; char line[MAXLINE]; //Input string printf("Enter a string:"); //Input string k=0; while((line[k]=getchar())!='\n') k++; line[k]='\0';//'\ 0' must be added artificially //Points to the first and last element positions, respectively. i=0; k-=1; while(i<k){ if(line[i]!=line[k])//If the corresponding characters are not equal, the cycle ends in advance. break; i++; k--; } //Judge whether the while loop ends normally. If so, it indicates that the string is a palindrome. if(i>=k) printf("It's palindrome!"); else printf("Not palindromes!"); return 0; }
2) Recursion
//recursion #include<stdio.h> #include<string.h> int judge(int low,int high,char *arr,int len){ //Final conditions. if(len==0 || len==1) return 1; if(arr[low]!=arr[high]) return 0; return judge(low+1,high-1,arr,len-2); } int main(){ char arr[10]="aaabbaaa"; int len=strlen(arr); if(judge(0,len-1,arr,len)) printf("Yes!"); else printf("no"); return 0; } /*The function of recursion is to reduce the scale of the problem until the problem is reduced to a simple solution Through observation, we can know that a palindrome string is also a palindrome inside. Therefore, we only need to check layer by layer in the form of removing the characters at both ends, Each check removes two characters, so as to reduce the scale of the problem. 1. The string length may be odd or even: If the string length is an odd number, the middle character of the string will remain, but it does not affect the palindrome. When it is checked that the length is 1, it means that the string is a palindrome If the string length is even, there will be no characters left after the string at both ends is compared and checked. That is, when the length is 0, it means that the string is a palindrome 2. If it is checked that the two characters at both ends are different. It indicates that this string is not a palindrome and directly returns 0. You do not need to continue checking -------- Copyright notice: This article is CSDN Blogger「cbsheng」Original articles, follow CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement. Original link: https://blog.csdn.net/cbs612537/article/details/8217425
3) Pointers are similar to array methods. Understand for yourself.
10. Square array transpose
#include<stdio.h> #define MAXN 6 int main(){ int i,j,n,temp; int a[MAXN][MAXN]; printf("ENTER n:"); scanf("%d",&n); for(i=0;i<n;i++){ for(j=0;j<n;j++){ a[i][j]=i*n+j+1;//Assign values to array elements } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(i<=j){ temp=a[i][j]; a[i][j]=a[j][i]; a[j][i]=temp; } } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("%4d",a[i][j]); } printf("\n"); } return 0; }
3, Small details
1. Each row of the two-dimensional array can be regarded as a one-dimensional array, and the address of the first element of the row can be directly represented by the array name + [line number].
2. Pay attention to the length of the array to prevent cross-border.
3. Two dimensional array is actually a special one-dimensional array. The storage mode of two-dimensional array is similar to one-dimensional array.
4. The array name of a two-dimensional array refers to the first address of the array, that is, the address of the first row.
5. The array name cannot be self increasing and self decreasing. The array name is an address constant, and such operation cannot be performed.
Please understand the problem thoroughly!
I hope you can learn something and wait for your correction and advice!!!