C Program to Multiply Two Matrices Using Multi-dimensional Arrays

This post multiplies two matrices using multi-dimensional arrays in the C programming language.

C Program to Multiply Two Matrices Using Multi-dimensional Arrays

#include <stdio.h>
int main()
{
    int m, n, p, q, i, j, k, sum = 0;
    int A[10][10], B[10][10], C[10][10];
    printf("Enter number of rows and columns of A matrix\n");
    scanf("%d %d", &m, &n);
    printf("Enter elements of A matrix\n");
    for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
    scanf("%d", &A[i][j]);
    printf("Enter number of rows and columns of B matrix\n");
    scanf("%d %d", &p, &q);
    if (n != p)
    printf("The multiplication of matrix not possible \n");
    else
    {
        printf("Enter elements of B matrix\n");
        for (i = 0; i < p; i++)
        for (j = 0; j < q; j++)
        scanf("%d", &B[i][j]);
        for (i = 0; i < m; i++) 
        {
            for (j = 0; j < q; j++) 
            {
                for (k = 0; k < p; k++) 
                {
                    sum = sum + A[i][k]*B[k][j];
                }
                C[i][j] = sum;
                sum = 0;
            }
        }
        printf("Product of the matrices:\n");
        for (i = 0; i < m; i++) 
        {
            for (j = 0; j < q; j++)
            printf("%d\t", C[i][j]);
            printf("\n");
        }
    }
    return 0;
    }

Output

Explanation

In this C program, we use many variables for storing various values and for performing different calculations. Here we have initialized three array values for doing the multiplication of matrices. We also allow the user to enter the values to the variables m, npq.

 	int m, n, p, q, i, j, k, sum = 0;
    int A[10][10], B[10][10], C[10][10];
    printf("Enter number of rows and columns of A matrix\n");
    scanf("%d %d", &m, &n);
    printf("Enter elements of A matrix\n");
    for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
    scanf("%d", &A[i][j]);
    printf("Enter number of rows and columns of B matrix\n");
    scanf("%d %d", &p, &q);

Now we have used the if-else condition to check whether the condition, satisfies the following calculation. We now utilize the variable sum to do the calculation, where we have previously addressed the sum as zero(sum=0). If the given is condition is satisfied then we will proceed towards the result and, if the given condition is not satisfied, then the multiplication will not be possibly done.

if (n != p)
    printf("The multiplication of matrix not possible \n");
    else
    {
        printf("Enter elements of B matrix\n");
        for (i = 0; i < p; i++)
        for (j = 0; j < q; j++)
        scanf("%d", &B[i][j]);
        for (i = 0; i < m; i++) 
        {
            for (j = 0; j < q; j++) 
            {
                for (k = 0; k < p; k++) 
                {
                    sum = sum + A[i][k]*B[k][j];
                }
                C[i][j] = sum;
                sum = 0;
            }
        }

We now proceed further for the fancied output, where the third array carries out the result of the multiplication.

 printf("Product of the matrices:\n");
        for (i = 0; i < m; i++) 
        {
            for (j = 0; j < q; j++)
            printf("%d\t", C[i][j]);
            printf("\n");
        }

We have now attained the desired output.