How to define and reference two-dimensional arrays

Table of contents

How to define a two-dimensional array

How to reference elements of a two-dimensional array

Two-dimensional array program example


How to define a two-dimensional array


In C language, you can use the following syntax to define a two-dimensional array:

```c
data_type array_name[row_size][column_size];
```

- `data_type` represents the data type of the elements in the two-dimensional array, which can be integers, floating point numbers, characters, etc.
- `array_name` is the name of the two-dimensional array you define and can be customized as needed.
- `row_size` represents the number of rows (also called vertical size) of the array, that is, how many rows are contained in the two-dimensional array.
- `column_size` represents the number of columns of the array (can also be called the horizontal size), that is, how many columns are included in each row of the two-dimensional array.

For example, here is an example that defines a two-dimensional array of integers with 3 rows and 4 columns:

```c
int matrix[3][4];
```

This will create a 2D integer array called `matrix` with 3 rows and 4 columns.

You can also initialize a two-dimensional array when you define it, just like you would a one-dimensional array. For example, to create a two-dimensional array of integers containing initial values:

```c
int matrix[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
```

In this example, we initialize a 3x4 two-dimensional array of integers, with each row containing 4 integers. You can provide a corresponding number of initial values ​​for each row in curly braces.

Note that each subarray (that is, each row) can have a different length, but within the same array, each row must have the same number of columns. In the example above, each row has 4 columns.

How to reference elements of a two-dimensional array


To reference an element of a two-dimensional array, you need to use two indexes: one to access the rows and another to access the columns. The element reference syntax of a two-dimensional array in C language is as follows:

```c
array_name[row_index][column_index]
```

- `array_name` is the name of the 2D array you defined.
- `row_index` is the row index of the element to be accessed, it starts from 0.
- `column_index` is the column index of the element to be accessed, also starting from 0.

For example, suppose you have a 3x4 two-dimensional array of integers called `matrix`, and you want to access the elements in the second row and third column, you can use the following way:

```c int element = matrix[ 1
][2]; // Get the elements in the second row and third column```

This will store the value of the element in the second row and third column in the `element` variable. In a two-dimensional array, the first index `1` represents the second row and the second index `2` represents the third column.

You can use similar methods to modify the element values ​​of a two-dimensional array. For example, to set the elements in the first row and second column to the new value `42`, you can do this:

```c
matrix[0][1] = 42; // Set the elements in the first row and second column to 42
```

To iterate over an entire 2D array, you can use nested loops, one for iterating over the rows and another for iterating over the columns. Here is an example:

```c
int matrix[3][4]; // Assume that the two-dimensional array has been initialized

for (int i = 0; i < 3; i++) { // Traverse rows
    for (int j = 0; j < 4; j++) { // Traverse columns
        printf("matrix[%d][%d] = % d\n", i, j, matrix[i][j]);
    }
}
```

This will access and print each element in the 2D array one by one.

Two-dimensional array program example


The following is a simple example program that demonstrates how to declare, initialize, and operate a two-dimensional integer array:

```c
#include <stdio.h>

int main() {     // Declare and initialize a 3x3 two-dimensional array of integers     int matrix[3][3] = {         {1, 2, 3},         {4, 5, 6},         {7, 8, 9 }     };





    // Traverse and print the elements of the two-dimensional array
    printf("Contents of the two-dimensional array: \n");
    for (int i = 0; i < 3; i++) { // Traverse the rows
        for (int j = 0; j < 3; j++) { // Iterate over columns
            printf("%d\t", matrix[i][j]); // Use tabs to separate elements
        }
        printf("\n"); // On each line Print newline character at the end
    }

    // Access and modify specific elements in the array
    int element = matrix[1][2]; // Get the element in the second row and third column (the value is 6)
    matrix[0][0] = 10; // Change The elements in the first row and first column are set to 10

    //Print the modified array
    printf("\nContents of the modified two-dimensional array:\n");
    for (int i = 0; i < 3; i++) {         for (int j = 0; j < 3 ; j++) {             printf("%d\t", matrix[i][j]);         }         printf("\n");     }




    return 0;
}
```

This program first declares and initializes a 3x3 two-dimensional array of integers `matrix`, and then uses a nested loop to iterate and print the elements of the array. Next, it demonstrates how to access and modify specific elements in the array, and finally prints the modified array again.

Running this program will output the following:

```
Contents of two-dimensional array:
1 2 3
4 5 6
7 8 9

Contents of the modified two-dimensional array:
10 2 3
4 5 6
7 8 9
```

This example helps you understand how to declare, initialize, and manipulate two-dimensional arrays. You can modify the size and contents of the array as needed.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132913551