codescracker
c

C Program to Multiply Two Matrices



« Previous Program Next Program »

In this article, you will learn and get code about the multiplication of two matrix in C. But before going through the program, if you are not aware about how multiplication of two matrix performs, then I recommend you to have a look at the step by step process of matrix multiplication. There, you will see the step by step process using pictorial representation, that how a multiplication of two matrix gets calculated. Now let's move on and implement it in a C program.

Matrix Multiplication in C

To multiply any two matrices in C programming, first ask from the user to enter any two matrix, then start multiplying the given two matrices, and store the multiplication result one by one inside any variable say sum. Store the value of sum in the third matrix (one by one as its element) say mat3 as shown in the program given here.

The question is, write a program in C that multiply two given matrices. The answer to this question is given below. This C program asks from user to enter any two 3*3 matrix elements, to multiply them to form a new matrix which is the multiplication result of two given 3*3 matrices. Here 3*3 matrix means, a matrix that has 3 rows and 3 columns:

#include<stdio.h>
#include<conio.h>
int main()
{
    int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
    printf("Enter first 3*3 matrix element: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            scanf("%d", &mat1[i][j]);
    }
    printf("Enter second 3*3 matrix element: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            scanf("%d", &mat2[i][j]);
    }
    printf("\nMultiplying two matrices...");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            sum=0;
            for(k=0; k<3; k++)
                sum = sum + mat1[i][k] * mat2[k][j];
            mat3[i][j] = sum;
        }
    }
    printf("\nMultiplication result of the two given Matrix is: \n");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
            printf("%d\t", mat3[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the output you will see on your screen. This is first snapshot:

c program multiply two matrices

Now supply any 9 elements for first 3*3 matrix, and then again 9 elements for second 3*3 matrix. After supplying all the 9-9 elements for both the 3*3 matrix, press ENTER to see the multiplication result of the two matrix as shown in the second screenshot of the sample run given here:

matrix multiplication in c

Program Explained

  1. Get first 9 elements or numbers from user and store it inside the first matrix, index wise from 00 to 22
  2. That is first element stored inside mat1[0][0], second element stored inside mat1[0][1], third element stored inside mat1[0][2], ....., seventh element stored inside mat1[2][0], eighth element stored inside mat1[2][1], and the last or ninth element stored inside mat1[2][2]
  3. In similar way, get second 9 elements from user and store it inside the second matrix
  4. Now We've total of two 3*3 matrix with 9-9 elements in each.
  5. Here We've used three for loop to multiply the matrices. The first two for loop is used for row and column, whereas the third one is used to apply the multiplication rule of matrix
  6. Apply the matrix multiplication rule and multiply the matrix, after getting multiplication result, store the value inside the sum variable, after multiplying each and every row elements of first matrix with corresponding each and every column elements of second matrix, and initialize the value of sum variable into the third matrix one by one. Never forgot to initialize 0 to sum before multiplying process starts for each index of third matrix
  7. In this way the third matrix say mat3 contains total of 9 elements that will be the multiplication result of the two given matrix say mat1 and mat2
  8. Finally print the value of third matrix

Allow User to Define Size of Matrix

Now let's modify the above program by implementing extra feature. That is, this program allows user to define the size of matrix:

#include<stdio.h>
#include<conio.h>
int main()
{
    int mat1[10][10], mat2[10][10], matmult[10][10];
    int row1, col1, row2, col2, i, j, k, sum;
    printf("Enter size of first matrix:\n");
    printf("Enter row size: ");
    scanf("%d", &row1);
    printf("Enter column size: ");
    scanf("%d", &col1);
    printf("\nEnter the element of first matrix:\n");
    for(i=0; i<row1; i++)
    {
        for(j=0; j<col1; j++)
            scanf("%d", &mat1[i][j]);
    }
    printf("\nEnter size of second matrix:\n");
    printf("Enter row size: ");
    scanf("%d", &row2);
    printf("Enter column size: ");
    scanf("%d", &col2);
    printf("\nEnter the element of second matrix:\n");
    for(i=0; i<row2; i++)
    {
        for(j=0; j<col2; j++)
            scanf("%d", &mat2[i][j]);
    }
    if(col1!=row2)
    {
        printf("\nMultiplication not possible!");
        printf("\nExiting...\n");
        printf("Press any key...");
        getch();
        return 0;
    }
    printf("\nMultiplying the two matrix...\n");
    for(i=0; i<row1; i++)
    {
        for(j=0; j<col2; j++)
        {
            sum = 0;
            for(k=0; k<col1; k++)
                sum = sum + mat1[i][k] * mat2[k][j];
            matmult[i][j] = sum;
        }
    }
    printf("The multiplication result (resultant matrix) is:\n");
    for(i=0; i<row1; i++)
    {
        for(j=0; j<col2; j++)
            printf("%d ", matmult[i][j]);
        printf("\n");
    }
    getch();
    return 0;
}

Before multiplying the two given matrix entered at run-time, we have applied an if statement to check whether the column size of first matrix is equal to the row size of second matrix or not.

If it is equal, then start multiplying and find out the result. And if it is not, then print some result like multiplication not possible!. Here is the sample run:

matrix multiplication c program

The snapshot after providing row and column size along with matrix elements (for first matrix):

multiply two matrices c

The snapshot after providing row and column size along with matrix elements (for second matrix). Because we have provided 3 as row size of second matrix, which is equal to the column size of first matrix:

multiply two matrix c program

Now let's take another sample run. Here in this case, let's suppose user has entered the column size of first matrix as 3 and row size of second matrix as 2 which is not equal:

c program matrix multiplication

As you can clearly see from the above sample runs, the matrix multiplication is not possible, if the column size of first matrix is not equal to the row size of the second matrix.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker