array
int a0,a1; -> This thing cannot be referenced through a loop. The name cannot be split. Arrays are introduced into all C languages
Array definition:
An array is a collection of data (variables) of the same type
int a0,a1…a99; -> If it is defined by an array - > a [100] represents 100 integer data
One dimensional array
Defined format
The definition of one-dimensional array follows the definition of variable (type of variable, name of variable;)
The definition of one-dimensional array is the same:
Type of variable [number of variables] name of variable;
Since it's written above, isn't our definition of array
int[10] a;
There is no type int[10] in c language, so this [10] can only be put later, so the definition of array is
Type of variable name of variable [number of variables];
Example:
int a[10]; Number of variables - > number of data representing the same class
a has two meanings:
1. The first address of the array is equivalent to a pointer to the first address of the array
2. Variable representing the entire array
int main() { int a[10]; int *p = a; sizeof(a); //40, because a is a variable that represents the entire array. sizeof(a) -> sizeof(int[10]) sizeof(p); //4/8 } //sizeof (array name): returns the number of bytes of memory space occupied by all elements of the array. //sizeof (pointer): returns the number of address bytes of the computer system. If it is a 32-bit system, it returns 4, and if it is a 64 bit system, it returns 8
Relevant requirements:
Variable type: all legal types in c language can be used, such as basic type and construction type
Variable name: it is OK to comply with the naming rules of c language
Number of variables: in the old compiler, there can only be constant expressions. Up to now, we can use a variable with definite value to give this number
Note: if you define an array with variables, you cannot initialize it
Example:
int a[5]; //It means that five variables with the same int are defined //If you want to use these five variables, just use this a directly //Typeof - > find the type of a variable //typeof(a) -> int[5] int * p = a; //typeof(p) -> int * //int[5] int * is the same in a great sense, but it is also different. Int * has no range, and int[5] determines the range
Storage of array in memory:
Introduction:
The array is defined. To use it, we also need to know the address of the array, that is, the storage of these data in memory
Memory distribution of array:
After the array is defined, each element in the array is in a continuous address space, and the second one is stored next to the first one
[the transfer of external chain pictures fails, and the source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-uUh8nu8C-1622254834325)(F:\Typora \ Guangdong embedded learning notes \ storage of array. png)]
Reference to array:
Use the index of the array to refer to the array
Array name [subscript]; The subscript starts from 0 and is less than the number of arrays
Examples
int a[10];
A [0] - > the first element of the array
A [1] - > the second element of the array
...
A [9] - > the tenth and last element of the array
Arrays are as like as two peas.
How to use variables, then how to use the elements of the array
Examples
int b;
b = 1024;
int a[10];
a[0] = 1024;
a[9] = 1025;
Like ordinary variables, it conforms to the rules of left value and right value
Therefore, the same elements of the array can be addressed
practice:
Define an array, manually enter the value in it, use the loop, and print out the array
int a[10]; int i; for(i = 0;i < 10;i++) { scanf("%d",&a[i]); } for(i = 0;i < 10;i++) { printf("%d\t",a[i]); } printf("\n");
int a[10];
scanf("%d",&a); // may not
typeof(a) -> int[10]
typeof(&a) -> typeof(&int[10]) -> int[10] * -> int **
Indicates the address of the entire array, and the address of the entire array represents the entire array
The first address of the array represents the first element of the array
For example:
A + 1 - > represents the second element
& A + 1 - > means skip this array
int a[10];
scanf("%d",a); a -> &a[0]
It's OK to write this way. a represents the first element and it's an address
*Key points:
a represents the first address and the address of the first element
Therefore, both left and right values are OK
* a = 1024; <-> a[0] = 1024
+ + a,a + + doesn't work
a + 1 is OK
Core thinking: Although a is the first address and a represents the address of the first element
And a itself is a variable
But since a represents the whole address, if you change it, the array will change
Therefore, a cannot be changed
So a =a + 1 doesn't work either
Array initialization:
After the array is defined, we can give the initial value directly
Type of variable name of variable [number of variables] = {};
After the array is defined, we initialize it all
For example:
int a[5] = {1,2,3,4,5}; //Do not exceed the standard char name[10] = "abcdefghi"; //String must be followed by \ 0
2 array can be partially initialized, and the parts not initialized later will be initialized to 0
For example:
int a[5] = {1,2,3}; // a[3] == 0 , a[4] == 0
Array is not initialized. The value inside is unknown. Many times, we can clear an array
int a[5] = {0};
*Note:
int a[5] = {0};
After unknown operations, this a is likely to be used, so there are various values in it
At this time, I want to clear this array again
Then many people will do this - > A = 0; This must not be done
This can only be done at the time of definition, that is, initialization
int a[5] = {0};
The rest of the time I want to set 0 can only cycle
Memset - > is actually implemented in a loop
If I want to initialize all the arrays, I don't need to give the number when defining the array. The compiler can guess how many you have
int a[] = {1,2,3,4,5}; //And int a[5] = {1,2,3,4,5}; As like as two peas
The length of the string (strlen) and the size of the array (sizeof)
There is a special existence in the array, string array, which involves two concepts, string length and array size
//Example 1: char name[16] = "penglei"; printf("%d\n",sizeof(name)); //Find the size of the array and output 16, indicating that the size of the array is 16 bytes printf("%d\n",strlen(name)); //Find the length of the string, encounter the end of \ 0, and the output result is 7 char name[] = "penglei\0henshuai"; printf("%d\n",sizeof(name)); //Find the size of the array and output 17. The end of the string must end with \ 0 printf("%d\n",strlen(name)); //Find the length of the string, encounter the end of \ 0, and the output result is 7
//Example 2: int i; char name[3] = {'a','b','c'}; printf("%d\n",sizeof(name)); //Find the size of the array and output 3, indicating that the size of the array is 3 bytes printf("%d\n",strlen(name)); //The result is unknown. I don't know how much is behind the array. I don't know when I can meet the end of \ 0 char name[3]; char name1[10]; for(i=0;i<7;i++) { scanf("%c",&name[i]); //-> {'a','b','c','d','e','f','g'} } name1[0] = 'h'; printf("%s\n",name); // Output result - > ABC printf("%s\n",name1); //Output result - > hefg
Array allocation does not depend on who defines it first
It depends on who is smaller, who is in front and opens up continuously
Both% s and strlen need to see \ 0 to end
name1 = "h"; //In c language, strings cannot be assigned directly, so this sentence is wrong We want the assignment string to be a byte by byte assignment only Only during initialization can it be directly equal to be careful: 2.c Although the code has some problems, it explains a lot of problems Be sure to take time to understand
practice:
1 find the sum of an array and print the largest and smallest
for(i = 0;i < n;i++) { sum += a[i]; //Update maximum and minimum values if(max < a[i]) { max = a[i]; } if(min > a[i]) { min = a[i]; } }
2 fiboracci sequence, print the sum of the first 20 items and the first 20 items
1 1 2 3 5 8 13 ...
int a[20] = {1,1};
sum = 2;
i = 2;
for(;i < 20;i++)
{
a[i] = a[i - 1] + a[i - 2];
sum += a[i];
}
for(i = 0;i < 20;i++)
{
printf("%d ",a[i]);
}
printf("\nsum = %d\n",sum);
3 given a sequence, judge whether the sequence is increasing
If yes, otherwise no
int a[10];
for(i = 0;i < 9;i++)
{
If (a [i] > = a [i + 1]) / / as long as there is such an item, it is not incremental
{
printf("no\n");
break;
//return 0;
}
}
if(9 == i) / / if there is no break, it is incremented
{
printf("yes\n");
}
4 given an ordered sequence of numbers, find one of the elements
1. Traverse - > find one by one
2 dichotomy - > compromise search
int arr[n];// Give this array yourself
int left = 0,right = n;
int mid = (left + right) / 2;
While (left < = right) / / I don't know how many times it will cycle. If we find a break, we also need to end it
{
/ / compromise search is to find the middle number a[mid]
If (arr [mid] > x) / / indicates that the mid position is on the right side of X. I want to move the right side
{
right = mid - 1;// At least the first one to the left of the mid
mid = (left + right) / 2;
}
Else if (arr [mid] < x) / / X is on the right of mid
{
left = mid + 1;
mid = (left + right) / 2;
}
else / / equal
{
printf("found \ n");
break;
}
}
if(left > right)
{
printf("not found \ n");
}
5 sorting algorithm
1 bubbling - > one bubbling will move the maximum value to the last
As long as the previous value is greater than the latter value, change the position
arr[10];
int i,j;
for(i = 0;i < 9;i++)
{
For (J = 0; J < 9; j + +) / / this is one of them
{
If (arr [J] > arr [J + 1]) / / as long as the following value is smaller than me, it needs to be exchanged
{
arr[j] = arr[j] ^ arr[j +1];
arr[j +1] = arr[j] ^ arr[j +1];
arr[j] = arr[j] ^ arr[j +1];
}
}
}
Job:
1 Select Sorting - > find out the maximum value that is not arranged in one trip and put it on the last side that is not arranged
We need to save the subscript of the maximum value
2 Find the second largest value in the array When updating the maximum value, you can change the maximum value before updating to the second largest value If a value is larger than the second largest value but smaller than the first largest value, update the second largest value directly 3 Place negative numbers in an array before positive numbers so easy!!! 4 Insert sorting, and the sequence will be arranged after the insertion is completed See 4.c 5 Random sorting -> Think for yourself Demand 1 ~ 100 Randomly distributed into an array of 100 elements The number in the array will not be repeated 1 int arr[100] = {0}; int i,j; srand((int)time(NULL)); for(i = 1;i <= 100;i++) { while(1) { //Random subscript j = rand() % 100;//Random can be a number between 0 and 99, and this number can be regarded as a subscript //The subscript has been randomly selected, so we can consider inserting this number into this subscript //When you insert it, the element should be 0. If it is not 0, you should not insert it if(0 == arr[j]) { arr[j] = i;//If the remaining number is very small, it is difficult to ensure that it can be inserted on time //There may not be random subscripts //This algorithm can only exist in theory break; } } } 2 There is no way to consider the position of 0 by improving the above method Improve the location of 0 Isn't the random position of 0 OK Just open an array to save the position of 0. The position of 0 is the insertion position int arr[100] = {0}; int add[100] = {0}; int num_0 = 0; int i,j,k; srand((int)time(NULL)); for(i = 1;i <= 100;i++) { num_0 = 0;//Set the number of 0 to 0 //Count the number of 0 and save the location of 0 for(j = 0,k = 0;j < 100;j++) { if(0 == arr[j])//Find a 0 { num_0++; add[k] = j;//Save the position of 0 in the add array k++;//After saving one, you need to save the next one later }//After this cycle, the position of 0 is saved and the number is saved } //The insertion position can be found by the number of random 0 arr[add[rand() % num_0]] = i; //Random location and find the real subscript of this place } 3 It's OK to continue the above, but it's a little difficult to think about The last unordered number randomly exchanges positions with the previous number It can be achieved by moving forward in turn //Write the third code
random number
There are no random numbers in the computer, and some are pseudo-random
srand((int)time(NULL)); // Divide a position
You only need to call it once. Of course, you can call it many times
rand()The return value of the function is a pseudo-random number How to output multiple random numbers in a loop
2 multidimensional array
Data type variable name;
Define array - > one-dimensional variable type [number] variable name;
->Variable type variable name [number];
How to define a two-dimensional array according to a one-dimensional array Variable type variable name[number]; ->One dimensional array Just add a two-dimensional dimension to one dimension Variable type[Number of two-dimensional] Variable name[number]; -> Variable type variable name[number][Number of two-dimensional]; The above is the definition of two-dimensional array Define format Data type array name[Row size][Column size] The size of rows and columns is given in the same way as the number in a one-dimensional array Generally, we give constant expressions. If you want to use variables, it's OK But with variables, you can't initialize The variable you use must have a definite value
When using two-dimensional array, our two-dimensional array is through one-dimensional array
Therefore, when using two-dimensional arrays, we can use one-dimensional arrays
In fact, a two-dimensional array is a one-dimensional array
Memory is a one-dimensional
Distribution of two-dimensional array in memory
See Figure
Reference elements in a two-dimensional array
Single element references array name [row] [column]
The rows and columns start from 0
Want to use a row of data array name [row]
->The array name [row] represents a row of data
Since a two-dimensional array is a one-dimensional array, can we make a pointer and directly use a single element in it? Sure a[0] ->The first line represented is the first address of the first line Q: a[3][5] int * p = a[0]; yes The elements in a row are int a[0]Represents the first address of this line a[0] = &a[0][0] typeof(&a[0][0]) -> int * //int * p = a[0]; int c; typeof(c) d = c; typeof(&a[0][0]) p = &a[0][0]; int * p = &a[0][0] = a[0] p = p + 5;//Yes p[5] Now? p To whom? a[1][0]
int * q = a; no
The whole array is represented by a
So there's a problem with that
Combine the two above
Assume a = 0x0010
Q:
a[0] + 1 = 0x0014
a + 1 = 0x0024 plus an int[5]
The actual performance is to jump to the second line
If a is a variable, can a get the address
&a + 1 = 0x0010 + 4 * 15
Skip entire array
Int a [3] [5] - > int [5] a [3] - > assuming int[5]=X
int[5] a[3] =>X a[3];
->X[3] a;
->int[5][3] a;
typeof(a) -> int[5][3]
typeof(&a) -> int[5][3] *
&a + 1 = 0x0010 + 15 * 4
int a[3]; a= &a[0] int * p = a;
Initialization of two-dimensional array
1 we can initialize the two-dimensional array
int a[2][3] = {
{1,2,3},
{4,5,6}
};
We can write the above form in a or brackets
int a[2][3] = {
1,2,3,4,5,6
};
3 Partial initialization. The remaining uninitialized ones will be automatically set to 0 int a[2][3] = { {1,2}, //The first line initializes 2 and the third is 0 {4,5} //The second line initializes 2, and the third line is 0 }; int a[2][3] = { 1,2, 4,5 }; -> The first line is all initialized 1 2 4 The second line initializes only one 5 0 0 4 If a two-dimensional array is fully initialized, we can omit it The number of rows, but columns cannot be omitted int a[][3] = { {1,2,3}, {4,5,6} }; perhaps int a[][3] = { 1,2,3,4,5,6 }; char name[][16] = { "penglei", "henshuai", ..... };
int function(int arr[][3])
{
}
int main()
{
int a[2][3];
function(a);
}
practice:
1 find the maximum and minimum of a two-dimensional array
And print out the sum of the array
for()
{
for()
{
...
}
}
2 Find the column and row with the largest sum of elements in a two-dimensional array int a[3][5]; //It should be the sum of the first row and the first column. I'm too lazy here int max_lin = a[0][0],max_col = a[0][0]; int sum = 0; int i,j; for(i = 0;i < 3;i++)//that 's ok { sum = 0;//Reset and for(j = 0;j < 5;j++) { sum += a[i][j]; } if(max_lin < sum)//Update large value { max_lin = sum; } } for(j = 0;j < 5;j++)//column { sum = 0; for(i = 0;i < 3;i++) { sum += a[i][j];//Find the value of a column } if(max_col < sum)//Update large value { max_col = sum; } }
3 find the top element
Mountaintop element: the element larger than the left, right, up and down is the mountaintop element
There is no need to consider, but it may also be a mountaintop element
In fact, we don't have to think so much
There are inert operations in c language
If ((left doesn't exist or I'm bigger than you) & & (right doesn't exist or I'm bigger than you)...)
{
/ / mountaintop element
}
int a[5][5]
if((j == 0 || a[i][j] > a[i][j - 1]) && (j == 4 || a[i][j] > a[i][j + 1])) && ((i == 0 || a[i][j] > a[i - 1][j]) && (i == 4 || a[i][j] > a[i + 1][j])) 6.c 4 Output the first 10 lines of Yanghui triangle Regardless of the one on the right 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 ..... int i,j; int a[10][10] = {0}; for(i = 0;i < 10;i++)//that 's ok { for(j = 0;j <= i;j++) { //We should first consider the case equal to 1 if(0 == j || i == j)// { a[i][j] = 1; } else { //If it is not 1, this value is equal to the one above it plus the one on the left above it a[i][j] = a[i -1][j] + a[i - 1][j - 1]; } printf("%4d ",a[i][j]); } printf("\n"); } 7.c
5. High data occupancy problem
There is a man named Gao data. He wants to take seats for him and his two roommates (3 people)
It has a habit that it can't be separated from its roommate and can't sit back and forth
Only three people can be connected together and some seats have been occupied
Write code to find out how many occupation methods they have
Go and give this seat yourself
if(!a[i][j] && !a[i][j+1] && !a[i][j+2] )
{
num++;
}
6 Find the largest subarray of a one-dimensional array(successive)And 2 -1 -3 4 5 -8 -9 10 11 -2 The largest subarray is 10 11 The sum is 21 1 Brute force crack all situations of liejue to get the maximum value 2 sum As long as it is added to the case less than 0, it is directly equal to 0 int a[10] = {1,-2,-3,4,5,3,-5,20,-30,10}; int sum = a[0]; int sum_max = sum; int i = 0; for( ;i < 10;i++) { sum += a[i]; if(sum < 0) { sum = 0; } if(sum_max < sum) { sum_max = sum;//Update max } } if(sum_max < 0) { //Just traverse the maximum value } 8.c
printf("\n");
}
7.c
5. High data occupancy problem
There is a man named Gao data. He wants to take seats for him and his two roommates (3 people)
It has a habit that it can't be separated from its roommate and can't sit back and forth
Only three people can be connected together and some seats have been occupied
Write code to find out how many occupation methods they have
Go and give this seat yourself
if(!a[i][j] && !a[i][j+1] && !a[i][j+2] )
{
num++;
}
6 Find the largest subarray of a one-dimensional array(successive)And 2 -1 -3 4 5 -8 -9 10 11 -2 The largest subarray is 10 11 And 21 1 Brute force crack all situations of liejue to get the maximum value 2 sum As long as it is added to the case less than 0, it is directly equal to 0 int a[10] = {1,-2,-3,4,5,3,-5,20,-30,10}; int sum = a[0]; int sum_max = sum; int i = 0; for( ;i < 10;i++) { sum += a[i]; if(sum < 0) { sum = 0; } if(sum_max < sum) { sum_max = sum;//Update max } } if(sum_max < 0) { //Just traverse the maximum value } 8.c