Lesson 41 of 60 – Two-Dimensional Array in C
68%

Two-Dimensional Array in C

A two-dimensional array stores data in rows and columns. It is commonly used to represent tables, matrices, marksheets, and other grid-like data.

Note: A two-dimensional array uses two indexes: the first index represents the row and the second index represents the column.

1. What is a Two-Dimensional Array?

A two-dimensional array is an array arranged in rows and columns.

int matrix[2][3];

This array has 2 rows and 3 columns.

        Column
          0   1   2
Row 0    [ ] [ ] [ ]
Row 1    [ ] [ ] [ ]

2. Syntax of a 2D Array

The basic syntax is:

data_type array_name[rows][columns];

Example:

int matrix[3][4];
  • int is the data type.
  • matrix is the array name.
  • 3 represents rows.
  • 4 represents columns.

3. Rows and Columns

Consider this array:

int matrix[2][3];

It contains:

  • 2 rows
  • 3 columns
  • 6 total elements

Total elements can be calculated as:

rows × columns
2 × 3 = 6

4. Indexing in a 2D Array

Both row and column indexes start from 0.

matrix[0][0]
matrix[0][1]
matrix[0][2]

matrix[1][0]
matrix[1][1]
matrix[1][2]

The first index identifies the row and the second identifies the column.

5. Initializing a 2D Array

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

The first group represents the first row and the second group represents the second row.

6. Visualizing a 2D Array

        Column
          0    1    2

Row 0    10   20   30

Row 1    40   50   60

For example, matrix[1][2] contains 60.

7. Accessing an Element

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

printf("%d", matrix[0][1]);

Output:

20

The first index selects row 0 and the second selects column 1.

8. Accessing the Last Element

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

printf("%d", matrix[1][2]);

Output:

60

For a 2 × 3 array, the last valid index pair is [1][2].

9. Changing an Element

int matrix[2][2] = {
    {10, 20},
    {30, 40}
};

matrix[1][0] = 100;

printf("%d", matrix[1][0]);

Output:

100

A specific element can be modified using its row and column indexes.

10. Displaying a 2D Array

Nested loops are commonly used to display all elements of a 2D array.

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

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        printf("%d ", matrix[i][j]);
    }

    printf("\n");
}

11. Nested Loops for 2D Arrays

A 2D array usually needs two loops:

  • Outer loop → rows
  • Inner loop → columns
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        printf("%d ", matrix[i][j]);
    }
}

12. Taking Input in a 2D Array

int matrix[2][3];

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        scanf("%d", &matrix[i][j]);
    }
}

Each input value is stored at its corresponding row and column.

13. Complete Input and Display Program

#include <stdio.h>

int main()
{
    int matrix[2][3];

    printf("Enter 6 numbers:\n");

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("Matrix:\n");

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", matrix[i][j]);
        }

        printf("\n");
    }

    return 0;
}

14. Sum of All Elements

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

int sum = 0;

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        sum += matrix[i][j];
    }
}

printf("Sum = %d", sum);

Output:

Sum = 210

15. Row-Wise Sum

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

for (int i = 0; i < 2; i++)
{
    int sum = 0;

    for (int j = 0; j < 3; j++)
    {
        sum += matrix[i][j];
    }

    printf("Row %d Sum = %d\n", i, sum);
}

Output:

Row 0 Sum = 60
Row 1 Sum = 150

16. Column-Wise Sum

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

for (int j = 0; j < 3; j++)
{
    int sum = 0;

    for (int i = 0; i < 2; i++)
    {
        sum += matrix[i][j];
    }

    printf("Column %d Sum = %d\n", j, sum);
}

Output:

Column 0 Sum = 50
Column 1 Sum = 70
Column 2 Sum = 90

17. Finding the Largest Element

int matrix[2][3] = {
    {10, 70, 30},
    {40, 50, 20}
};

int largest = matrix[0][0];

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        if (matrix[i][j] > largest)
        {
            largest = matrix[i][j];
        }
    }
}

printf("Largest = %d", largest);

Output:

Largest = 70

18. Finding the Smallest Element

int matrix[2][3] = {
    {10, 70, 30},
    {40, 50, 20}
};

int smallest = matrix[0][0];

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        if (matrix[i][j] < smallest)
        {
            smallest = matrix[i][j];
        }
    }
}

printf("Smallest = %d", smallest);

Output:

Smallest = 10

19. Matrix Addition

Two matrices of the same dimensions can be added element by element.

int a[2][2] = {
    {1, 2},
    {3, 4}
};

int b[2][2] = {
    {5, 6},
    {7, 8}
};

int result[2][2];

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 2; j++)
    {
        result[i][j] = a[i][j] + b[i][j];
    }
}

20. Matrix Addition Output

Consider:

A =  1  2
     3  4

B =  5  6
     7  8

The result is:

R =  6   8
    10  12

Each corresponding pair of elements is added.

21. Transpose of a Matrix

The transpose of a matrix changes rows into columns and columns into rows.

Original:

1  2  3
4  5  6

Transpose:

1  4
2  5
3  6

The element at matrix[i][j] becomes transpose[j][i].

22. Transpose Program

#include <stdio.h>

int main()
{
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int transpose[3][2];

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            transpose[j][i] = matrix[i][j];
        }
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            printf("%d ", transpose[i][j]);
        }

        printf("\n");
    }

    return 0;
}

23. Passing a 2D Array to a Function

A two-dimensional array can be passed to a function. For a fixed column size, the column dimension must be known in the parameter declaration.

void display(int matrix[][3], int rows)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", matrix[i][j]);
        }

        printf("\n");
    }
}

24. Complete Function with 2D Array

#include <stdio.h>

void display(int matrix[][3], int rows)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%d ", matrix[i][j]);
        }

        printf("\n");
    }
}

int main()
{
    int matrix[2][3] = {
        {10, 20, 30},
        {40, 50, 60}
    };

    display(matrix, 2);

    return 0;
}

25. Searching in a 2D Array

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

int search = 50;
int found = 0;

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        if (matrix[i][j] == search)
        {
            found = 1;
            printf("Found at row %d, column %d",
                   i, j);
            break;
        }
    }

    if (found)
    {
        break;
    }
}

26. Row and Column Indexes

For an array declared as:

int matrix[3][4];

The valid row indexes are:

0, 1, 2

The valid column indexes are:

0, 1, 2, 3

Therefore, the last valid element is:

matrix[2][3]

27. Common 2D Array Mistakes

  • Using an invalid row index.
  • Using an invalid column index.
  • Mixing up rows and columns.
  • Using incorrect loop limits.
  • Forgetting that both indexes start from 0.
  • Using the wrong column size when passing a 2D array to a function.
int matrix[2][3];

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        printf("%d ", matrix[i][j]);
    }
}

28. Complete Matrix Addition Program

#include <stdio.h>

int main()
{
    int a[2][2];
    int b[2][2];
    int result[2][2];

    printf("Enter first matrix:\n");

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter second matrix:\n");

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            scanf("%d", &b[i][j]);
        }
    }

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            result[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("Result:\n");

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            printf("%d ", result[i][j]);
        }

        printf("\n");
    }

    return 0;
}

29. Practical Uses of 2D Arrays

Two-dimensional arrays are useful for many types of data:

  • Matrix calculations
  • Student marksheets
  • Tables of numbers
  • Game boards
  • Seating layouts
  • Grid-based programs
  • Rows and columns of data

For example, a marksheet can store students in rows and subjects in columns.

30. Practice Programs on 2D Arrays

Practice the following programs:

  1. Create and display a 2 × 2 matrix.
  2. Take input for a 3 × 3 matrix.
  3. Display all elements using nested loops.
  4. Find the sum of all elements.
  5. Find the largest element.
  6. Find the smallest element.
  7. Calculate row-wise sums.
  8. Calculate column-wise sums.
  9. Add two matrices.
  10. Find the transpose of a matrix.
  11. Search for an element in a matrix.
  12. Pass a 2D array to a function and display it.

📌 Key Points

  • A two-dimensional array stores data in rows and columns.
  • A 2D array uses two indexes.
  • The first index represents the row.
  • The second index represents the column.
  • Both row and column indexes start from 0.
  • Nested loops are commonly used to process 2D arrays.
  • Rows and columns can be processed separately.
  • Two matrices of the same dimensions can be added element by element.
  • A matrix can be transposed by exchanging row and column positions.
  • A 2D array can be passed to a function with the required column dimension specified.

🧠 Quick Quiz

Question: How many indexes are required to access an element of a two-dimensional array?