Skip to content
Home ยป Two-Dimensional Arrays

Two-Dimensional Arrays

โœ… Two-Dimensional Arrays (2D Arrays)


โœ… 1. Definition of Two-Dimensional Array

A Two-Dimensional Array (2D Array) is a collection of elements of the same data type stored in the form of a table (rows and columns).

โœ… It is also called a Matrix Array.

๐Ÿ“Œ Example:
It looks like a grid:

Row\Col   0    1    2
0        10   20   30
1        40   50   60
2        70   80   90

โœ… 2. Declaration of 2D Array

โœ… Syntax (C Language)

datatype array_name[rows][columns];

โœ… Example:

int A[3][3];

This means:

  • 3 rows
  • 3 columns
  • Total elements = 3 ร— 3 = 9

โœ… 3. Initialization of 2D Array

โœ… Example:

int A[2][3] = {
    {10, 20, 30},
    {40, 50, 60}
};

Another way:

int A[2][3] = {10,20,30,40,50,60};

โœ… 4. Accessing Elements in 2D Array

Elements are accessed using two indexes:

โœ… Format:

A[i][j]

Where:

  • i = row number
  • j = column number

โœ… Example:

  • A[0][0] = 10
  • A[1][2] = 60

โœ… 5. Memory Representation of 2D Array

A 2D array is stored in contiguous memory, but the elements are stored row-wise in C.

โœ… (A) Row Major Order (Used in C / C++)

In Row Major Order, all elements of first row are stored first, then second row, and so on.

โœ… Example:

A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2]

โœ… Address Calculation Formula (Row Major)

[
LOC(A[i][j]) = Base(A) + [(i \times N) + j] \times W
]

Where:

  • Base(A) = Base address of array
  • N = Number of columns
  • W = Size of each element
  • i = row index
  • j = column index

โœ… 6. Traversing a 2D Array

To traverse a 2D array, we use nested loops.

โœ… Example Program (Traversal):

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

โœ… 7. Operations on 2D Arrays

โœ… 1. Insertion

In 2D arrays, insertion means assigning a value to any cell:

โœ… Example:

A[1][1] = 99;

โœ… 2. Deletion

Deletion is not direct like linked list. We can:

  • Replace value by 0 or -1
  • Shift elements (complex)

โœ… Example:

A[1][1] = 0;

โœ… 3. Searching

Search an element by scanning all values:

โœ… Example:

for(i=0;i<rows;i++)
{
    for(j=0;j<cols;j++)
    {
        if(A[i][j] == key)
        {
            //found
        }
    }
}

Time Complexity: โœ… O(rows ร— cols)


โœ… 8. Applications of 2D Arrays

2D arrays are used in many real-life and programming problems:

โœ… Applications:

  1. Matrix representation
  2. Student marks table
  3. Image processing (pixels)
  4. Games (chess board, tic-tac-toe)
  5. Graph representation (Adjacency Matrix)

โœ… 9. Advantages of 2D Arrays

โœ… Benefits:

  • Easy representation of matrix data
  • Fast access using index
  • Easy to use with loops
  • Suitable for tables and grids

โœ… 10. Disadvantages of 2D Arrays

โŒ Limitations:

  • Fixed size (static memory)
  • Memory wastage if not fully used
  • Insertion and deletion are difficult
  • Requires contiguous memory allocation

โœ… Conclusion

A two-dimensional array stores data in row and column form like a table. It is very useful for representing matrix data such as marks, images, and graphs. In C language, 2D arrays are stored in row-major order, and elements are accessed using two indexes A[i][j].