One or one-D arrays
1. What is an array
2. Array syntax
3. Subscript
4. Initialization
5. Array name and the first address of the group
Application of two or one-D array
1. Assignment and copying of arrays
2. Positive and negative traversal of arrays
3. Random number
4. Array Chaos sequence
5, the repetition of the array
Three or two-D arrays
1, array of arrays
2. Initialization of two-dimensional arrays
3. Array names for two-dimensional arrays
Application of four or two-D array
1, two-dimensional array and two-dimensional table
2. Subscript Calculation
One or one-D arrays
1. What is an array
? An array is a data structure used to store multiple identical data types. Container
Analyze key points:
-Multiple strips
-Same type
-the array is the container for the data, not the data itself
-Each data in an array, called an element, consists of multiple elements (data). -Data Subscript (index) to differentiate elements in the data
-the element starts at position 0 of the array
-Elements can use the array variable name [subscript] to get concrete elements-the number of elements in the array is called the length of the array
-Space occupied by the array = type of element * Array length
-Array is a contiguous area of memory pointer
-array variable points to the area, is the array in memory of the first address
2. Array syntax
? Define array: Element type array name [number of elements];
int array[3];
? Access element: array name [subscript]
Array[0] = 10;
3. Subscript
? Subscript refers to the number in brackets when an array element is accessed
? The subscript cannot be crossed out, and if it is out of bounds, the following consequences may result:
– Program crashes directly
– Momin changes the values of other variables or data spaces
– Nothing happened.
4. Initialization
? Value assigned to use
int array2[3]={3,4,5};//assigns values to array elements in turn
int array4[3]={1,2};//If the number of assignments is less than the length of the array, the following values are automatically populated with zeros
? Assign 0 value
int array[3];//is not initialized when there may be garbage
Each element in an int array3[3]={0};//array is 0
5. Array name and the first address of the group
? The array name is an address, and it is an immutable address
? The array name cannot be an lvalue, or it cannot appear to the left of an assignment statement. Except for initialization.
? The array name is a constant (pointer). So you can't assign a value
Application of two or one-D array
1. Assignment and copying of arrays
? assigning values to arrays
int array[10];
ARRAY[2] = 10;//assigns the third element of the array to a value of 10
? Copy of array
int array[10] = {1,2,3,4,5,6,7,8,9,0};
int num [10];
for (int i = 0; i <; i++)
Num[i] = Array[i];
2. Positive and negative traversal of arrays
? Forward traversal of arrays
int array[10] = {1,2,3,4,5,6,7,8,9,0};
for (int i = 0; i <; i++)
printf ("%d", array[i]);
? Inverse traversal of an array
for (int i = 9; I >= 0; i--)
printf ("%d", array[i]);
3. Random number
? The rand () function is used to generate a random number
? The Srand () function is used to define random seeds
int main () {
Srand ((unsigned) time (0));
for (int i=0; i<10; i++) {
int x = rand ()% 100;
}
printf ("%d", x);
printf ("\ n");
return 0;
}
4. Array Chaos sequence
? Array shuffle refers to the random arrangement of elements in an array
int main () {
int array[10] = {1,2,3,4,5,6,7,8,9,0};
Srand ((unsigned) time (0));
for (int i=0; i<10; i++) {
int x = rand ()% (10-i) + i;
int temp = Array[i];
Array[i] = array[x];
}
ARRAY[X] = temp;
for (int i=0; i<10; i++)
printf ("%d", array[i]);
return 0;
}
5, the repetition of the array
? The repetition of an array element refers to the existence of the same array element in the array
Three or two-D arrays
1, array of arrays
? Define array: Element type array name [number of elements] [number of elements];
int array[3][4];
? Access element: array name [subscript] [subscript]
ARRAY[1][2] = 10;
2. Initialization of two-dimensional arrays
? corresponding element content
int array2[3][2]={{1,2},{3,4},{5,6}};
? Overall assignment
int array2[3][2]={1,2,3,4,5,6};
? Assign value auto-complement 0
int array2[3][2]={1,2,3,4};
3. Array names for two-dimensional arrays
? The two-dimensional array name is also an address, and it is an immutable address
? Two-dimensional array names are also not allowed to be left, or to the left of an assignment statement. Except for initialization.
? A two-dimensional array name is a constant (pointer). So you can't assign a value
Application of four or two-D array
1, two-dimensional array and two-dimensional table
? A two-dimensional array is actually an array of arrays
? A two-dimensional array can be understood as a two-dimensional table with several rows and columns.
2. Subscript Calculation
? The storage method of two-dimensional array in memory is to store the column and then save the release.
Eg:int Array[3][4];
The order in which the memory is stored is:
ARRAY[0][0]
ARRAY[0][1]
ARRAY[0][2]
ARRAY[0][3]
ARRAY[1][0]
ARRAY[1][1]
ARRAY[1][2]
ARRAY[1][3]
ARRAY[2][0]
ARRAY[2][1]
ARRAY[2][2]
ARRAY[2][3]
C Basic grammar--arrays