In programming, arrays are used to organize several variables of the same type in an orderly manner for the convenience of processing. These collections of similar data elements arranged in order are called arrays. In C language, arrays are constructed data types. An array can be decomposed into multiple array elements, which can be basic data types or constructed types. Therefore, according to the type of array elements, arrays can be divided into various categories such as numerical arrays, character arrays, pointer arrays, and structure arrays.
This chapter introduces numeric arrays and character arrays, and the rest will be introduced in subsequent chapters. Array type specification Before using an array in C language, a type specification must be performed first. The general form of array description is:
Type specifier array name [constant expression],...;
Among them, the type specifier is any basic data type or constructed data type. Array name is a user-defined array identifier. The constant expression in square brackets represents the number of data elements, also known as the length of the array.
For example:
int a[10]; indicates that the integer array a has 10 elements.
float b[10],c[20]; It means that the real array b has 10 elements and the real array c has 20 elements.
char ch[20]; Description character array ch, has 20 elements.
The following points should be noted when describing array types:
1. The type of an array actually refers to the value type of the array elements. For the same array, the data type of all its elements is the same.
2. The writing rules for array names should comply with the writing rules for identifiers.
3. The array name cannot be the same as other variable names, for example:
void main()
{
int a;< /p>
float a[10];
…
}
is wrong.
4. The constant expression in square brackets represents the number of array elements. For example, a[5] means that array a has 5 elements. But its subscript starts from 0. Therefore, the five elements are a[0], a[1], a[2], a[3], a[4].
5. Variables cannot be used in square brackets to represent the number of elements, but they can be symbolic constants or constant expressions. For example:
#define FD 5
void main()
{
int a[3+2],b[7 +FD];
…
}
is legal. But the following explanation is wrong.
void main()
{
int n=5;
int a[n];
……
}
6. Allow multiple arrays and multiple variables to be specified in the same type specification.
For example: int a,b,c,d,k1[10],k2[20];
Representation method of array elements
The array elements are The basic unit that makes up an array. Array elements are also variables, identified by the array name followed by a subscript. The subscript represents the sequence number of the element in the array. The general form of array elements is: array name [subscript] where the subscript can only be an integer constant or an integer expression. If it is a decimal, the C compiler will automatically round it. For example, a[5], a[i+j], a[i++] are all legal array elements. Array elements are also often called subscript variables. An array must be defined before subscripted variables can be used. In C language, subscript variables can only be used one by one, and the entire array cannot be referenced at once.
For example, to output an array with 10 elements, you must use a loop statement to output each subscript variable one by one:
for(i=0; i<10; i++) printf("%d",a[i] );
Instead of using one statement to output the entire array, the following writing is wrong:
printf("%d",a);
void main()
{
int i,a[10];
for(i=0;i<10;)
a[i++]=2*i+1;
for(i=9;i>=0;i--)
printf("%d",a[ i]);
printf("\n%d %d\n",a[5.2],a[5.8]);
}
for(i=0;i<10;)
a[i++]=2*i+1;
for(i=9;i>=0;i-- )
printf("%d",a[i]);
printf("\n%d %d\n",a[5.2],a[5.8] );
In this example, a loop statement is used to send odd values ??to each element of array a, and then a second loop statement is used to output each odd number from large to small. In the first for statement, expression 3 is omitted. The expression i++ is used in the subscript variable to modify the loop variable. Of course, the second for statement can also be done like this. C language allows expressions to be used to express subscripts. The last printf statement in the program outputs the value of a[5] twice. It can be seen that when the subscript is not an integer, it will be automatically rounded. Assignment to an array In addition to assigning values ??to the array elements one by one using assignment statements, initialization assignment and dynamic assignment can also be used. Array initialization assignment Array initialization assignment means assigning initial values ??to array elements during array declaration. Array initialization is performed during the compilation phase. This will reduce runtime and increase efficiency.
The general form of initialization assignment is: static type specifier array name [constant expression] = {value, value...value}; where static means a static storage type, and C language stipulates that only static storage arrays Only initialization assignments can be made with external storage arrays (regarding static storage, the concept of external storage is introduced in Chapter 5). Each data value in { } is the initial value of each element, and each value is separated by commas. For example: static int a[10]={ 0,1,2,3,4,5,6,7,8,9 }; is equivalent to a[0]=0;a[1]=1...a [9]=9;
C language also has the following provisions for the initial assignment of arrays:
1. You can only assign initial values ??to some elements. When the number of values ??in { } is less than the number of elements, only the first part of the elements are assigned values. For example: static int a[10]={0,1,2,3,4}; means that only the five elements a[0]~a[4] are assigned values, and the next five elements are automatically assigned the value 0.
2. You can only assign values ??to elements one by one, but not to the entire array. For example, assigning a value of 1 to all ten elements can only be written as:
static int a[10]={1,1,1,1,1,1,1,1,1,1} ;
but cannot be written as:
static int a[10]=1;
3. If an initial value is not assigned to an initializable array, then All elements have a value of 0.
4. If all elements are assigned a value, the number of array elements does not need to be given in the array description. For example:
static int a[5]={1,2,3,4,5};
can be written as:
static int a[ ]={1,2,3,4,5};
Dynamic assignment can dynamically assign values ??to an array during program execution. At this time, a loop statement can be used with the scanf function to assign values ??to the array elements one by one.
void main()
{
int i,max,a[10];
printf("input 10 numbers: \n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<10;i++)
if(a[i]>max) max=a[i];
printf("maxmum=%d\n",max);
}
for(i=0;i<10;i++) p>
scanf("%d",&a[i]);
max=a[0];
for(i=1;i<10;i++ )
if(a[i]>max) max=a[i];
printf("maxmum=%d\n",max);
< p> The first for statement in this example program inputs 10 numbers one by one into array a. Then send a[0] into max. In the second for statement, a[1] to a[9] are compared with the contents of max one by one. If it is larger than the value of max, the subscript variable is sent to max, so max is always in the The largest of the compared subscript variables. The comparison ends and the value of max is output.void main()
{
int i,j,p,q,s,a[10];
printf ("\n input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i] );
for(i=0;i<10;i++){
p=i;q=a[i];
for(j =i+1;j<10;j++)
if(q if(i !=p) { s=a[i]; a[i]=a[p]; a[p]=s; } printf("%d",a[i]); } } for(i=0;i<10;i++) scanf("%d",&a[i]); for(i =0;i<10;i++){ p=i;q=a[i]; for(j=i+1;j<10;j++)< /p> if(q if(i!=p) { s=a[i]; a[i]=a[p]; a[p]=s; printf("%d",a[i]); } This example program uses two parallel for loops statement, and a loop statement is nested in the second for statement. The first for statement is used to enter the initial value of 10 elements. The second for statement is used for sorting. The sorting of this program adopts the method of comparison one by one. During the i cycle, the subscript i of the first element is assigned to p, and the subscript variable value a[i] is assigned to q. Then enter a small loop, and compare each element from a[i+1] to the last element with a[i]. If there is one larger than a[i], its subscript will be sent to p, and the element value will be sent to q. After a loop ends, p is the subscript of the largest element, and q is the element value. If i≠p at this time, it means that the values ??of p and q are no longer the values ??assigned before entering the small loop, then the values ????of a[i] and a[p] are exchanged. At this time a[i] is the sorted element. After outputting this value, go to the next loop. Sort the elements after i+1. There are also two-dimensional arrays and multi-dimensional arrays that cannot be posted. First, understand the one-dimensional array. Two-dimensional and multi-dimensional are the same, which means they are divided downward