2D Array with Matrix Multiplication in C Programming

Introduction

A double subscripted variable, also known as a two-dimensional array, is a variable that has two subscripts assigned to it in order to represent a list of items. So to access a two-dimension array you have to have one row index and one column index. 2D arrays are widely used for representing grids, tables, matrices, and other structured data in C programming.

Consider two-dimensional arrays like Array A and Array B of n rows and n columns, input the elements in both arrays by the user, and perform their multiplications.

Source Code

#include<stdio.h>
#include<conio.h>

void main()
{
    int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q, k;

    clrscr();

    printf("Enter order of matrix A for m & n\n");
    scanf("%d%d", &m, &n);

    for(i = 0; i < m; i++)
    {
        for(j = 0; j < n; j++)
        {
            printf("Enter a[%d][%d] element: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }

    printf("Enter order of matrix B for p & q\n");
    scanf("%d%d", &p, &q);

    for(i = 0; i < p; i++)
    {
        for(j = 0; j < q; j++)
        {
            printf("Enter b[%d][%d] element: ", i, j);
            scanf("%d", &b[i][j]);
        }
    }

    if(n == p)
    {
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < q; j++)
            {
                c[i][j] = 0;
                for(k = 0; k < m; k++)
                    c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
            }
        }

        printf("A MATRIX\n");
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                printf("%5d", a[i][j]);
            }
            printf("\n");
        }

        printf("B MATRIX\n");
        for(i = 0; i < p; i++)
        {
            for(j = 0; j < q; j++)
            {
                printf("%5d", b[i][j]);
            }
            printf("\n");
        }

        printf("Elements after multiplication\n");
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < q; j++)
            {
                printf("%5d", c[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("Multiplication Not Possible for given matrices!");
    }

    getch();
}

Output

Output 2D Array

2D Array in C

Code Explanation

The program prompts the user to enter the order of matrices A and B. It then reads the elements of matrices A and B. It checks if the matrices can be multiplied (the number of columns of A is equal to the number of rows of B). If multiplication is possible, it calculates the product matrix C and displays the matrices A, B, and the resulting matrix C.

Summary

2D arrays are widely used for representing grids, tables, matrices, and other structured data in C programming. The program prompts the user to enter the order (number of rows and columns) of matrix A. It then prompts the user to enter the elements of matrix A. Next, the program prompts the user to enter the order of matrix B. It reads the elements of matrix B. The program checks if the matrices can be multiplied (the number of columns of A is equal to the number of rows of B). If multiplication is possible: It calculates the product matrix C using nested loops to perform matrix multiplication. It displays matrices A and B, followed by the resulting matrix C. If multiplication is not possible, it prints a message indicating that multiplication cannot be performed. The program uses the C standard library functions printf, scanf, clrscr, and getch for input and output operations.


Similar Articles