C Programming • Arrays & Functions
C Programming / Arrays (2-D)

Arrays (2-D)

Notes 3 Arrays & Functions

Definition: A two-dimensional array is an array arranged in the form of rows and columns. It is commonly used to represent tables and matrices.

Notes

Two-Dimensional Arrays (2-D)

Definition: A two-dimensional array is an array arranged in the form of rows and columns. It is commonly used to represent tables and matrices.

💡 Example: int marks[3][4]; creates a 2-D integer array with 3 rows and 4 columns.

Real-World Example

A classroom marks table can be represented using a 2-D array, where rows represent students and columns represent subjects.

💡 Example:

Row → Student
Column → Subject
Each cell → Marks of one student in one subject

2-D Array Representation

        Column
          0    1    2
       ┌────┬────┬────┐
Row 0  │ 10 │ 20 │ 30 │
       ├────┼────┼────┤
Row 1  │ 40 │ 50 │ 60 │
       ├────┼────┼────┤
Row 2  │ 70 │ 80 │ 90 │
       └────┴────┴────┘
📌 Important: Like 1-D arrays, 2-D array indexes in C start from 0.

Rows and Columns

Rows represent the horizontal entries of the array, while columns represent the vertical entries.

💡 Example: In int matrix[3][4]:

Number of rows = 3
Number of columns = 4
Total elements = 3 × 4 = 12

Declaration of 2-D Array

Definition: A 2-D array is declared by specifying the data type, array name, number of rows and number of columns.

data_type array_name[rows][columns];
💡 Example: int matrix[2][3]; declares an integer array with 2 rows and 3 columns.

Initialization of 2-D Array

Definition: Initialization means assigning values to the elements of a 2-D array when it is declared.

int matrix[2][3] = {
    {10, 20, 30},
    {40, 50, 60}
};
💡 Example: The first row contains 10, 20, 30 and the second row contains 40, 50, 60.

Accessing 2-D Array Elements

Definition: An element of a 2-D array is accessed using two indexes: one for the row and one for the column.

array_name[row][column]
💡 Example: For int matrix[2][3] = {{10,20,30},{40,50,60}};

matrix[0][0]10
matrix[0][2]30
matrix[1][1]50

2-D Array Indexing

        Column Index
           0    1    2
        ┌────┬────┬────┐
Row 0   │ 10 │ 20 │ 30 │
        ├────┼────┼────┤
Row 1   │ 40 │ 50 │ 60 │
        └────┴────┴────┘

matrix[0][0] = 10
matrix[0][1] = 20
matrix[0][2] = 30

matrix[1][0] = 40
matrix[1][1] = 50
matrix[1][2] = 60

Nested Loops and 2-D Arrays

A 2-D array is commonly processed using nested loops. The outer loop controls rows and the inner loop controls columns.

💡 Example:

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]);
    }
}

Taking Input in a 2-D Array

Definition: Input for a 2-D array can be taken element by element using nested loops.

💡 Example: scanf("%d", &matrix[i][j]); stores the entered value at row i and column j.

Displaying a 2-D Array

Definition: A 2-D array can be displayed row by row by using nested loops.

💡 Example: After printing each row, printf(" "); moves to the next row.

Practical Example — Read and Display a Matrix

Problem Statement

Write a C program to accept a 2 × 3 matrix from the user and display the matrix in row and column form.

Learning Outcomes

  • Declare and use a two-dimensional array.
  • Use nested loops to read matrix elements.
  • Use row and column indexes to access array elements.

Hint

Use two nested loops. The outer loop should handle rows and the inner loop should handle columns.

Theory

A 2-D array stores data in rows and columns. Nested loops provide a convenient way to process every element of the matrix.

Program

#include <stdio.h>

int main()
{
    int matrix[2][3];
    int i, j;

    printf("Enter 6 elements: ");

    // read matrix elements row by row
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 3; j++)
            scanf("%d", &matrix[i][j]);
    }

    printf("Matrix:
");

    // display matrix row by row
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 3; j++)
            printf("%d ", matrix[i][j]);

        printf("
");
    }

    return 0;
}

Expected Output

Enter 6 elements: 10 20 30 40 50 60
Matrix:
10 20 30
40 50 60

Modifying an Element

Definition: A particular element of a 2-D array can be changed by assigning a new value to its row and column index.

matrix[1][2] = 100;
💡 Example: If matrix[1][2] originally contains 60, the above statement changes it to 100.

Traversing a 2-D Array

Definition: Traversing a 2-D array means visiting every element row by row and column by column.

💡 Example: For a 2 × 3 matrix, traversal visits: [0][0] → [0][1] → [0][2] → [1][0] → [1][1] → [1][2]

Matrix Addition

Definition: Matrix addition is performed by adding corresponding elements of two matrices having the same dimensions.

💡 Example:

A = 1 2
    3 4

B = 5 6
    7 8

Result = 6 8
        10 12

Practical Example — Addition of Two Matrices

Problem Statement

Write a C program to accept two 2 × 2 matrices and calculate their sum.

Learning Outcomes

  • Use two-dimensional arrays to represent matrices.
  • Traverse matrices using nested loops.
  • Add corresponding elements of two matrices.

Hint

Read both matrices using nested loops. Add corresponding elements and store the result in a third matrix.

Theory

Two matrices can be added when they have the same number of rows and columns. Each element of the first matrix is added to the element at the same position in the second matrix.

Program

#include <stdio.h>

int main()
{
    int a[2][2], b[2][2], sum[2][2];
    int i, j;

    printf("Enter elements of first matrix: ");

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

    printf("Enter elements of second matrix: ");

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

    // add corresponding elements
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
            sum[i][j] = a[i][j] + b[i][j];
    }

    printf("Sum of matrices:
");

    // display the result matrix
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
            printf("%d ", sum[i][j]);

        printf("
");
    }

    return 0;
}

Expected Output

Enter elements of first matrix: 1 2 3 4
Enter elements of second matrix: 5 6 7 8
Sum of matrices:
6 8
10 12

1-D Array vs 2-D Array

1-D Array 2-D Array
Uses one index. Uses two indexes.
Usually represents a linear list. Usually represents rows and columns.
Example: int a[5]; Example: int a[3][4];
Processed using one loop. Usually processed using nested loops.

Common Mistakes in 2-D Arrays

Mistake Correct Understanding
Starting indexes from 1 Both row and column indexes start from 0.
Using wrong row/column limit For [3][4], rows are 0–2 and columns are 0–3.
Using one loop only Nested loops are normally used for complete traversal.
Confusing matrix[i][j] First index represents row, second represents column.

Important Points for Exam

  • A 2-D array stores data in rows and columns.
  • Two indexes are used to access an element.
  • Both indexes start from 0 in C.
  • Nested loops are commonly used to process a 2-D array.
  • Matrix addition requires matrices of the same dimensions.
  • For matrix[rows][columns], valid row indexes are 0 to rows - 1 and column indexes are 0 to columns - 1.
🎯 Easy Rule:

Outer loop → Row
Inner loop → Column
matrix[row][column]

Quick Revision

Concept Remember
2-D Array Data arranged in rows and columns.
Declaration data_type name[rows][columns];
Access array[row][column]
Indexing Starts from 0 for both dimensions.
Traversal Usually done with nested loops.
Matrix Addition Add corresponding elements.

Important Exam Questions

Short Answer Questions

  1. What is a two-dimensional array?
  2. Write the syntax for declaring a 2-D array.
  3. How are elements of a 2-D array accessed?
  4. What are rows and columns in a 2-D array?
  5. Why are nested loops used with 2-D arrays?
  6. What is the last valid row index of an array declared as int a[3][4]?
  7. What is the last valid column index of an array declared as int a[3][4]?
  8. What is matrix addition?

Long Answer Questions

  1. Define a two-dimensional array and explain its declaration, initialization and indexing with examples.
  2. Explain how nested loops are used to input and display a 2-D array.
  3. Write a C program to read and display a 2 × 3 matrix.
  4. Write a C program to add two matrices.
  5. Differentiate between one-dimensional and two-dimensional arrays.
🎥 Recommended Learning

Watch a beginner-friendly explanation of 2-D arrays, rows, columns and matrix operations in C.

▶ Watch: 2-D Arrays in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for 2-D arrays, indexing and matrix operations will be provided here.

🧠 Mind Map

Use the mind map for quick revision of rows, columns, indexing, traversal and matrix addition.