C language one-dimensional array - learning twelve

array

  • An array is a set of ordered data. The arrangement of each data in the array is regular, and the subscript represents the serial number of the data in the array.
  • Each element in the array belongs to the same data type.
  • Arrays must be defined before use.
  • definition
    • Type character array name [constant expression]

One dimensional array

  • General form of definition
    • Type character array name [constant expression];
    • For example: int a [4 + 6]; int b[10];
  • Reference the representation of array elements
    • Array name [subscript] -For example: a[1] = 10; a[0]=a[5]+a[7]-a[2*3];
  • Note: only array elements can be referenced, and the values of all elements of the whole array cannot be called at one time

Copy of array

#include<stdio.h>

void main()
{
    int a[10] = { 1,2,3,4,5,6,7,8,9,10 }, b[10];

    for (int i = 0; i < 10; i++){
        b[i] = a[i];
        printf("%d ", b[i]);
    }
}

Assignment of one-dimensional array

  • How to assign a value to an array:
    • Assign values to array elements one by one with assignment statements;
    • Use initialization assignment;
    • Dynamic assignment (scanf()).
  • Array initialization assignment:
    • Assign initial values to array elements when defining an array. Array initialization is performed during the compilation phase. This will reduce running time and improve efficiency.
  • General form
    • Type specifier array name [constant expression] = {value, value,..., value};
    • For example: int a [10] ={0,1,2,3,4,5,6,7,8,9}; int a [10] ={0,1,2,3,4};
  • If you want to set the value of all elements in an array to 0.
    • It can be written as: int a[10]={0,0,0,0,0,0,0,0,0}; Or int a[10]={0};
  • When assigning initial values to all array elements, you can not specify an array.
    • For example: int a[5]={1,2,3,4,5}; It can be written as: int a[]={1,2,3,4,5};
  • Dynamic assignment
    • Dynamic assignment can be made to logarithmic groups during program execution. At this time, you can assign values to the array elements one by one with the circular statement and the scanf function.
    • For example:
    for( i=0;i<10;i++)
         scanf_s( "%d", &a[il);

example

Assign values to 10 array elements as 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 in turn, and input them in reverse order

#include<stdio.h>

void main()
{
    int a[10], i;

    for (i = 0; i < 10; i++)
        a[i] = i;
    for (i=9; i >= 0 ; i--)
        printf("%d ",a[i]);
}

Exchange the elements in the array, exchange the first element of the array with the last one, exchange the second element with the penultimate one, and so on.

#include<stdio.h>

void main()
{
    int a[10], i,j;

    for (i = 0; i <= 9; i++)
        a[i] = i;
    for (i = 0; i < 10 / 2; i++) {
        j = a[i];
        a[i] = a[9 - i];
        a[9 - i] = j;
    }
    for(i=0;i<=9;i++)
        printf("%d ",a[i]);
}

Find the largest element and its subscript in a one-dimensional array.

#include<stdio.h>

void main()
{
    int a[10], i,j,max;

    for (i = 0; i < 10; i++)
        scanf_s("%d",&a[i]);
    max = a[0];
    for (i = 0; i <10; i++)
        if (a[i]>max){
        max = a[i];
        j = i;
    }
    printf("max=%d,j=%d\n", max, j);

}

Using array to solve the problem of Fibonacci sequence

#include<stdio.h>

void main()
{
    int i; 
    int f[10] = { 1,1 }; 
    for (i = 2; i < 10; i++)
        f[i] = f[i - 2] + f[i - 1]; 
    for (i = 0; i < 10; i++)
    {
        printf("%12d\n",f[i]);
    }
}

practice

practice

1. The following description of one-dimensional a is correct.

(A)int a(10);

(B)int n=10,a[n];

(C)int n;scanf("%d",&n);

(D)

 #define SIZE 10 
 int a[SIZE];

2. The following statement that can correctly initialize one-dimensional array a is _.

(A) int a[10]= (0,0,0,0,0);

(B)int a[10] = {};

(C)int a[] = {0};

(D)int a[10] = {10*1};

3. When you enter 18 from the keyboard and press enter, the running result of the following program is _.

#include<stdio.h>

main()
{
    int x, y, i, a[8], j, u, v; 
    scanf_s("%d", &x);
    y = x;
    i = 0;
    do {
        u = y / 2;
        a[i] = y % 2;
        i++;
        y = u;
    }while (y >= 1); 
        for (j = i - 1; j >= 0; j--)
            printf("%d", a[j]);
    }

(A)10010

(B)10001

(C)01001

(D)00110

answer

1,D   2,C   3,A  

Added by melittle on Mon, 28 Feb 2022 15:49:02 +0200