C - Multidimensional Array

Multidimensional arrays allow you to store data in a tabular format with multiple dimensions. The most common type of multidimensional array is a two-dimensional array, which represents a matrix or a table-like structure. 

Declaration and Initialization:

To declare and initialize a two-dimensional array, you specify the number of rows and columns in the array.

int matrix[3][4];   // Declaration of a 3x4 two-dimensional array
int table[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};   // Declaration and initialization of a 2x3 two-dimensional array

In this example, matrix is declared as a 3x4 two-dimensional array, and table is declared as a 2x3 two-dimensional array and initialized with specific values.

Accessing Elements:

You can access individual elements of a two-dimensional array using the row and column indices.

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};
int element = matrix[1][2];   // Access the element at row 1, column 2

In this example, element will have the value 6, which is the element at the second row and third column of the matrix array.

Looping through a 2D Array:

You can use nested loops to iterate through the elements of a two-dimensional array.

int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
int rows = 3;
int cols = 3;
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        printf("%d ", matrix[i][j]);   // Print each element of the array
    }
    printf("\n");   // Move to the next row
}

In this example, nested loops are used to iterate through each element of the matrix array and print its value. The outer loop iterates over the rows, and the inner loop iterates over the columns. Here's how you can work with arrays in C: