C language two-dimensional array - learning thirteen

This article was last updated on February 5, 2022, and has not been updated for more than 22 days. If the article content or picture resources fail, please leave a message and feedback. I will deal with it in time. Thank you!

Two dimensional array

  • General form:
    • Type specifier array name [constant expression] [constant expression];
  • for example
    • int a[3][4]; Describes an array with three rows and four columns. The array name is a and the type of its subscript variable is integer.
  • Storage of two-dimensional array in memory
    • Two dimensional array is two-dimensional in concept, but the actual hardware memory is continuously addressed, that is, the memory units are arranged in one-dimensional linear array.
    • In C language, a two-dimensional array is arranged in rows, that is, after one row is placed, it is placed in the second row in sequence.

Reference of two-dimensional array

  • General form:
    • Array name [subscript] [subscript]
  • for example
    • b[1][2]=a[2][3]/2;
  • Subscripts can be integer expressions.
    • For example: a[2][3], a[2-1][2*2-1]
  • The subscript value should be within the defined array size.
    • For example: int a[3][4]; a[3][4]=3; Wrong definition a is an array of 3 rows and 4 columns. The subscript starts from 0. The maximum subscript of the row is 2 and the maximum subscript of the column is 3

Initialization of two-dimensional array

  • Assign initial values to the two-dimensional array.
    • For example: int a [3] [4] = {1,2,3,4}, {5,6,7,8}, {9,10,11,12};
  • Write all data in a flower bracket and assign initial values to each element in the order of array arrangement.
    • For example: int a [3] [4] = {1,2,3,4,5,6,7,8,9,10,11,12};
  • Assign initial values to some elements.
    • For example: int a [3] [3] = {1}, {2}, {3}; Assign a value to the first column element of each row, and the unassigned element takes the value of 0.
    • After assignment
      • 1 0 0
      • 2 0 0
      • 3 0 0
  • If all elements are given initial values, the length of the first dimension may not be specified when defining the array, but the length of the second dimension cannot be omitted.
    • For example: int a [3] [4] = {1,2,3,4,5,6,7,8,9,10,11,12};
    • It can be written as: int a[][4]={{1,2,3,4,5,6,7,8,9,10,11,12};

example

There is a 3 × 4 matrix, which requires programming to find the value of the element with the largest value, as well as its row number and column number.

#include<stdio.h>

main()
{
    int max, a[3][4],i,j,x,y; 
    max = a[0][0];
    printf("Please enter data\n");
    for (i = 0; i <= 2; i++){
        for (j = 0; j <= 3; j++) {
            scanf_s("%d", &a[i][j]);
        }
    }
    for (i = 0; i <= 2; i++) {
        for (j = 0; j <= 3; j++) {
            if (max < a[i][j]) {
                max = a[i][j];
                x = i+1;
                y = j+1;
            }
        }
    }
    printf("max=%d,In the first%d OK, page%d column", max, x, y);

}

Exchange the elements of rows and columns of a two-dimensional array and store them in another two-dimensional array.

#include<stdio.h>

main()
{
    int a[2][4],b[4][2],i,j; 

    printf("Please enter data\n");
    for (i = 0; i <= 1; i++){
        for (j = 0; j <= 3; j++) {
            scanf_s("%d", &a[i][j]);
            
        }
    }
    printf("\n");

    for (i = 0; i <= 1; i++) {
        for (j = 0; j <= 3; j++) {
            b[j][i] = a[i][j];
            printf("%d ", a[i][j]);
            }
        printf("\n");
    }
    printf("\n");

    for (i = 0; i <= 3; i++) {
        for (j = 0; j <= 1; j++) {
            printf("%d ", b[i][j]);
        }
        printf("\n");
    }
}

The diagonal of the two-dimensional array a10 is 1 and the others are 0.

#include<stdio.h>

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

    for (i = 0; i <= 9; i++){
        for (j = 0; j <= 9; j++) {
            if (i == j||i+j==9) {
                a[i][j] = 1;
            }
            else
            {
                a[i][j] = 0;
            }
        }
    }
    printf("\n");

    for (i = 0; i <= 9; i++) {
        for (j = 0; j <= 9; j++) {
            printf("%d ", a[i][j]);
            }
        printf("\n");
    }
}

Print out the following 10 lines of Yang Hui triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
#include<stdio.h>

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

    for (i = 0; i <= 9; i++){
        for (j = 0; j <= i; j++) {
            if (j == 0 || j == i) {
                a[i][j] = 1;
            }
            else
            {
                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            }
        }
    }
    printf("\n");

    for (i = 0; i <= 9; i++) {
        for (j = 0; j <= i; j++) {
            printf("%d ", a[i][j]);
            }
        printf("\n");
    }
}

Added by Arryn on Mon, 28 Feb 2022 15:48:44 +0200