2-Dimensional and Multidimensional Arrays in C Practice Questions with Solutions

Introduction

Two-dimensional arrays in C are used to store data in rows and columns, similar to a table or matrix. Multidimensional arrays extend this idea to three or more dimensions. In this chapter, you will practice 2D and multidimensional arrays through solved C programs, including matrix input and output, addition, subtraction, diagonal elements, transpose, searching, and 3D arrays. These examples are designed to build a strong foundation before moving to strings and pointers. 2-Dimensional and Multidimensional Arrays in C practice questions with solutions to help you understand the concepts.

Q1. Create and Print a 2D Array

Problem Statement

Write a C program to create a 2 × 3 two-dimensional array and print all its elements in rows and columns.

C Program

#include <stdio.h>

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

    int i, j;

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

        printf("\n");
    }

    return 0;
}

Sample Output

10 20 30
40 50 60

Explanation

This declaration:

int numbers[2][3];

creates a 2D array with:

  • 2 rows
  • 3 columns
  • 6 total elements

You can visualize it as:

        Column
        0   1   2

Row 0   10  20  30
Row 1   40  50  60

To access an element, use:

numbers[row][column]

For example:

numbers[0][1]

contains 20.

The outer loop handles rows, while the inner loop handles columns.

Concepts Covered

  • Two-dimensional arrays
  • Rows and columns
  • Nested for loops
  • Array indexing

Q2. Take Input for a 2D Array

Problem Statement

Write a C program to take input for a 2 × 3 array and display the entered matrix.

C Program

#include <stdio.h>

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

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

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

    printf("\nMatrix:\n");

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

        printf("\n");
    }

    return 0;
}

Sample Output

Enter 6 numbers:
10
20
30
40
50
60

Matrix:
10 20 30
40 50 60

Explanation

The array is declared as:

int numbers[2][3];

The nested loops take input for every position.

For example:

numbers[0][0]
numbers[0][1]
numbers[0][2]
numbers[1][0]
numbers[1][1]
numbers[1][2]

The same nested-loop structure can then be used to print the matrix.

Concepts Covered

  • 2D array input
  • Nested loops
  • scanf()
  • Matrix output

Q3. Find the Sum of All Elements in a 2D Array

Problem Statement

Write a C program to calculate the sum of all elements in a 3 × 3 matrix.

C Program

#include <stdio.h>

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

    int i, j;
    int sum = 0;

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

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

    return 0;
}

Sample Output

Sum = 45

Explanation

The matrix is:

1 2 3
4 5 6
7 8 9

Every element is added:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
= 45

The nested loops visit every element.

Concepts Covered

  • 2D array traversal
  • Nested loops
  • Sum calculation
  • Matrix elements

Q4. Add Two Matrices

Problem Statement

Write a C program to add two 2 × 2 matrices.

C Program

#include <stdio.h>

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

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

    int result[2][2];
    int i, j;

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

    printf("Result:\n");

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

        printf("\n");
    }

    return 0;
}

Sample Output

Result:
6 8
10 12

Explanation

The first matrix is:

1 2
3 4

The second matrix is:

5 6
7 8

We add corresponding elements:

1 + 5 = 6
2 + 6 = 8
3 + 7 = 10
4 + 8 = 12

The important statement is:

result[i][j] = a[i][j] + b[i][j];

Concepts Covered

  • Matrix addition
  • Multiple 2D arrays
  • Nested loops
  • Element-by-element operations

Q5. Subtract Two Matrices

Problem Statement

Write a C program to subtract one 2 × 2 matrix from another matrix.

C Program

#include <stdio.h>

int main()
{
    int a[2][2] = {
        {10, 20},
        {30, 40}
    };

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

    int result[2][2];
    int i, j;

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

    printf("Result:\n");

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

        printf("\n");
    }

    return 0;
}

Sample Output

Result:
9 18
27 36

Explanation

The program subtracts corresponding elements:

10 - 1 = 9
20 - 2 = 18
30 - 3 = 27
40 - 4 = 36

The calculation is performed using:

result[i][j] = a[i][j] - b[i][j];

Concepts Covered

  • Matrix subtraction
  • 2D arrays
  • Nested loops
  • Arithmetic operations

Q6. Find the Sum of Each Row

Problem Statement

Write a C program to find the sum of every row in a 3 × 3 matrix.

C Program

#include <stdio.h>

int main()
{
    int matrix[3][3] = {
        {10, 20, 30},
        {5, 15, 25},
        {2, 4, 6}
    };

    int i, j;
    int sum;

    for (i = 0; i < 3; i++)
    {
        sum = 0;

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

        printf("Sum of row %d = %d\n", i + 1, sum);
    }

    return 0;
}

Sample Output

Sum of row 1 = 60
Sum of row 2 = 45
Sum of row 3 = 12

Explanation

The matrix is:

10 20 30
5  15 25
2  4  6

For the first row:

10 + 20 + 30 = 60

For the second row:

5 + 15 + 25 = 45

For the third row:

2 + 4 + 6 = 12

The variable sum is reset to 0 at the beginning of every row.

Concepts Covered

  • Row traversal
  • Nested loops
  • Row-wise calculation
  • 2D arrays

Q7. Find the Main Diagonal Sum

Problem Statement

Write a C program to find the sum of the main diagonal elements of a 3 × 3 matrix.

C Program

#include <stdio.h>

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

    int i;
    int sum = 0;

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

    printf("Main diagonal sum = %d", sum);

    return 0;
}

Sample Output

Main diagonal sum = 15

Explanation

The main diagonal contains elements where the row index and column index are the same:

1  2  3
   5
      9

The diagonal elements are:

1, 5, 9

Therefore:

1 + 5 + 9 = 15

The important expression is:

matrix[i][i]

When i changes:

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

Concepts Covered

  • Matrix diagonal
  • 2D array indexing
  • Row and column indexes
  • Nested array concepts

Q8. Find the Transpose of a Matrix

Problem Statement

Write a C program to find the transpose of a 2 × 3 matrix.

C Program

#include <stdio.h>

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

    int transpose[3][2];
    int i, j;

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

    printf("Transpose:\n");

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

        printf("\n");
    }

    return 0;
}

Sample Output

Transpose:
1 4
2 5
3 6

Explanation

Original matrix:

1 2 3
4 5 6

It has:

2 rows × 3 columns

After transpose:

1 4
2 5
3 6

It becomes:

3 rows × 2 columns

The important statement is:

transpose[j][i] = matrix[i][j];

The row and column indexes are swapped.

For example:

matrix[0][1] = 2

becomes:

transpose[1][0] = 2

Concepts Covered

  • Matrix transpose
  • Swapping row and column indexes
  • 2D arrays
  • Nested loops

Q9. Search for an Element in a 2D Array

Problem Statement

Write a C program to search for a number in a 3 × 3 matrix.

C Program

#include <stdio.h>

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

    int search;
    int i, j;
    int found = 0;

    printf("Enter number to search: ");
    scanf("%d", &search);

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (matrix[i][j] == search)
            {
                printf("Number found at row %d, column %d\n",
                       i + 1, j + 1);

                found = 1;
            }
        }
    }

    if (found == 0)
    {
        printf("Number not found.");
    }

    return 0;
}

Sample Output

Enter number to search: 60
Number found at row 2, column 3

Explanation

The nested loops visit every element.

For example:

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

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

and so on.

When:

matrix[i][j] == search

is true, the position is displayed.

For 60:

Internal index:
row = 1
column = 2

Because users normally count rows and columns starting from 1, the program prints:

row 2, column 3

Concepts Covered

  • Searching a 2D array
  • Nested loops
  • Row and column indexes
  • Conditional statements

Q10. Create and Print a Three-Dimensional Array

Problem Statement

Write a C program to create a three-dimensional integer array and print all its elements.

C Program

#include <stdio.h>

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

    int i, j, k;

    for (i = 0; i < 2; i++)
    {
        printf("Block %d:\n", i + 1);

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

            printf("\n");
        }

        printf("\n");
    }

    return 0;
}

Sample Output

Block 1:
1 2
3 4

Block 2:
5 6
7 8

Explanation

A three-dimensional array is declared as:

int numbers[2][2][2];

It contains:

2 × 2 × 2 = 8 elements

You can think of it as two separate 2 × 2 matrices:

Block 1:

1 2
3 4

and:

Block 2:

5 6
7 8

Because there are three dimensions, we use three indexes:

numbers[i][j][k]

Therefore, three nested loops are used.

Concepts Covered

  • Three-dimensional arrays
  • Multiple indexes
  • Three nested loops
  • Multidimensional arrays

Key Takeaways

  • A two-dimensional array stores data in rows and columns.
  • A 2D array is commonly used to represent matrices and tables.
  • Array indexes start from 0.
  • matrix[row][column] is used to access a 2D array element.
  • Two nested loops are commonly used to process a 2D array.
  • Matrix addition and subtraction are performed element by element.
  • A matrix transpose swaps rows and columns.
  • Diagonal elements can be accessed using matching row and column indexes.
  • Multidimensional arrays can have three or more dimensions.
  • A 3D array requires three indexes.
  • The total number of elements is obtained by multiplying the size of every dimension.
  • Accessing an array outside its valid bounds can result in undefined behavior.
  • 2D arrays are an important foundation for matrices, tables, and more advanced C programming.

FAQs

1. What is a two-dimensional array in C?

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

Example:

int matrix[3][4];

This creates an array with 3 rows and 4 columns.

2. How many elements are in int matrix[3][4]?

There are:

3 × 4 = 12

elements.

3. How do you access an element of a 2D array?

Use two indexes:

matrix[row][column]

For example:

matrix[1][2]

accesses the element in row index 1 and column index 2.

4. Why are nested loops used with 2D arrays?

A 2D array has rows and columns, so one loop can process the rows and another loop can process the columns.

5. What is a multidimensional array in C?

An array having more than one dimension is called a multidimensional array.

Examples:

int a[3][4];
int b[2][3][4];

The first has two dimensions and the second has three.

6. What is a 3D array in C?

A three-dimensional array has three indexes.

Example:

int numbers[2][3][4];

An element can be accessed using:

numbers[i][j][k]

7. What is the difference between a 1D and 2D array?

A 1D array uses one index:

numbers[i]

A 2D array uses two indexes:

matrix[i][j]

A 1D array can be visualized as a list, while a 2D array can be visualized as a table.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top