C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

Two Dimensional Array In C


In C programming a 2D array is also called a Matrix.. The elements of 2D arrays are arranged in rows and columns. 

The element of the matrix can be accessed by using its row index and column index, for example: The value 120 can be accessed using its row and column index as : arr [2][3].

2D array Declaration  

In C programming 2D array can be declared using the following syntax:

datatype arrayName[rowSize][colSize];

Here,

- Data type can be any valid data type like: int, float, char etc.

- rowSize is the dimension of the row

- columnSize is the dimension of the column.

Based on the above syntax, we can create a matrix of size 2 X 3 as follows:

// matrix of size 2 X 3. It can store total of 6 elements
int arr[2][3];

2D array is a  basically array of arrays, in 2D array each array element is also an array. Let us say we have to store 8 elements: 10,20,30,40,50,60,70 and 80. We can use 2D array to store these elements as:

 

2D array initialization in C

In C programming, the 2D array can be declared and initialized using following ways:

int arr[3][2] = {
                    {10,20},
                    {30,40},
                    {50,60}
                };
// The above caan also be written as:
int arr[3][2] = {10,20,30,40,50,60};

While declaring 2D arrays, it is always required to define size of second dimension i.e column, while the size of first dimension i.e row is optional. It can be used as follows:

char c[2][3] = {'a','b','c','d','e','f'};
//the first dimension is optional, and can be written as
char c[][3] = {'a','b','c','d','e','f'};

Write a C program, to store 6 elements and display the elements in matrix form.

#include <stdio.h>

int main() {
    int matrix[10][10],row, col;

    // Ask the user for row and column size
    printf("Enter the number of rows: ");
    scanf("%d", &row);
    printf("Enter the number of columns: ");
    scanf("%d", &col);

    // Ask the user to enter the elements
    printf("Enter %d elements:\n", row * col);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            printf("Element at [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    // Display the elements in matrix form
    printf("Matrix:\n");
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows: 3
Enter the number of columns: 3
Enter 9 elements:
Element at [0][0]: 10
Element at [0][1]: 20
Element at [0][2]: 30
Element at [1][0]: 40
Element at [1][1]: 50
Element at [1][2]: 60
Element at [2][0]: 70
Element at [2][1]: 80
Element at [2][2]: 90
Matrix:
10	20	30	
40	50	60	
70	80	90

Write a C program to find largest element in 2D array i.e matrix

#include <stdio.h>

int main() {
    int matrix[10][10],row, col;

    // Ask the user for row and column size
    printf("Enter the number of rows: ");
    scanf("%d", &row);
    printf("Enter the number of columns: ");
    scanf("%d", &col);

    // Ask the user to enter the elements
    printf("Enter %d elements:\n", row * col);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Find the largest element in the matrix
    int largest = matrix[0][0];
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            if (matrix[i][j] > largest) {
                largest = matrix[i][j];
            }
        }
    }

    // Display the largest element
    printf("The largest element in the matrix is: %d\n", largest);

    return 0;
}

Output:

Enter the number of rows: 2
Enter the number of columns: 3
Enter 6 elements:
2
3
10
5
9
7
The largest element in the matrix is: 10

Write a C program to multiply two given matrix [Matrix Multiplication in C]

#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10

int main() {
    int a[MAX_ROWS][MAX_COLS], b[MAX_ROWS][MAX_COLS], c[MAX_ROWS][MAX_COLS];
    int m,n,p,q;

    printf("Enter the number of rows and columns for matrix 1: ");
    scanf("%d %d", &m, &n);

    printf("Enter the number of rows and columns for matrix 2: ");
    scanf("%d %d", &p, &q);

    if (n != p) {
        printf("Matrix multiplication not possible!\n");
        return 1;
    }

    printf("Enter the elements of matrix 1:\n");
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter the elements of matrix 2:\n");
    for (int i = 0; i < p; i++) {
        for (int j = 0; j < q; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    // Multiply matrices
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < q; j++) {
            c[i][j] = 0;
            for (int k = 0; k < n; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    // Display the result
    printf("Resultant Matrix:\n");
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < q; j++) {
            printf("%d\t", c[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter the number of rows and columns for matrix 1: 2 2
Enter the number of rows and columns for matrix 2: 2 2
Enter the elements of matrix 1:
1 1
1 1
Enter the elements of matrix 2:
1 1
1 1
Resultant Matrix:
2	2	
2	2