What is a Two-Dimensional (2D) Array?

A two-dimensional (2D) array, also known as a matrix, is a data structure in programming that consists of elements organized in rows and columns. It can be thought of as a table or grid of values, where each element is uniquely identified by its row and column indices. In a 2D array:

  • Rows: The horizontal sequences of elements are called rows. Each row contains a collection of related data, and it is identified by a numerical index.
  • Columns: The vertical sequences of elements are called columns. Each column contains elements of the same type or category, and it is also identified by a numerical index.
  • Elements: Each cell within the 2D array holds a single value, which can be of any data type, such as integers, floating-point numbers, characters, or even other arrays.

The notation used to access an element in a 2D array typically involves specifying both the row and column indices, separated by square brackets. For example, array[row][column] would access the element at the specified row and column.

Here's a simple example of a 2D array in C that represents a 3x3 matrix:


int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
    

In this example, matrix is a 2D array with three rows and three columns, and each element can be accessed using two indices: matrix[row][column]. The element at matrix[0][0] is 1, and the element at matrix[2][2] is 9.

2D arrays are commonly used to represent tables of data, grids, images, and matrices in various programming applications, including numerical computations, image processing, and games. They provide an organized and efficient way to work with data that has a two-dimensional structure.

Declaration of a 2D Array:


#include <stdio.h>

int main() {
    // Declaration of a 2D array
    int matrix[3][3];

    // Initialization of a 2D array
    // Using nested loops to assign values
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            matrix[i][j] = (i + 1) * (j + 1);
        }
    }

    // Printing the 2D array
    printf("2D Array (Matrix):\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}
    

Explanation:

  1. Declaration of a 2D Array:

    We declare a 2D integer array named matrix with dimensions 3x3. This means it has 3 rows and 3 columns.

  2. Initialization of a 2D Array:

    We use nested loops to initialize the elements of the 2D array. The nested loops traverse the rows and columns of the array.

    In this example, we use a simple formula (i + 1) * (j + 1) to assign values to each element based on its row and column indices. This will result in a 3x3 matrix with values:

                1  2  3
                2  4  6
                3  6  9
                
  3. Printing the 2D Array:

    After initializing the 2D array, we use nested loops again to print its contents.

    The format %2d is used to print each integer with a minimum width of 2 characters, which aligns the columns neatly.