C Program To Multiply 2 Matrix.



Hello Friends.
Welcome to my Blog...!!
In this post, we will see something really fascinating i.e. How to Multiply 2 Matrix using C Language.

Let’s revise the basic concept that are must require for multiplying 2 Matrices.

We know that a Matrix is an Array of numbers. The concept of dot product is used for multiplying 2 matrices.

When we perform multiplication of 2 matrices it’s necessary that the number of Column of 1st Matrix must equal to the number of rows of 2nd Matrix. And hence, the resultant Matrix will have the same number of Rows as that of 1st Matrix & same number of Column as that of 2nd Matrix.

Hence in general,
Let M1 be a Matrix having ‘a’ Rows and ‘b’ Columns &
Let M2 be a Matrix having ‘b’ Rows and ‘c’ Columns.
M1=a*b   & M2=b*c        =>  M3=M1*M2=a*c

Let’s take a Sweet & Simple example:

We will hence include this necessary condition in our code after taking the value of r1,c1(Row & Column of Matrix1) r2,c2(Row & Column of Matrix2) and check whether the value of c1&r2 is same or not by using the While loop. If the values will be same then the Execution of While loop will be neglected & If they aren’t same then the While loop will again ask for the values of r1,c1 r2,c2 until the value of c1&r2 get same. And hence after getting correct input of r1,c1 r2,c2 the process of Multiplication of 2 Matrices start.

So Come-on,  let’s see the Code:
#include<stdio.h>
#include<conio.h>

int main()
{
    clrscr();
    int A[10][10],B[10][10],C[10][10], r1,c1,r2,c2,i,j,k;
    int sum=0;
    printf("Enter the rows for first matrix : ");
    scanf("%d",&r1);
    printf("\nEnter the columns for first matrix : ");
    scanf("%d",&c1);
    printf("\nEnter the rows for second matrix : ");
    scanf("%d",&r2);
    printf("\nEnter the columns for second matrix : ");
    scanf("%d",&c2);

    while(c1!=r2)
    {
 printf("\nError..!! Column No. of Matrix A is not equal to Row No. of Matrix B");
 printf("\nEnter the Rows and Columns for Matrix A : ");
 scanf("%d %d",&r1,&c1);
 printf("\nEnter the Rows and Columns for Matrix B : ");
 scanf("%d %d",&r2,&c2);
    }

//This loop will intake the values of Matrix A

    printf("Enter Elements for Matrix A :\n");
    for(i=0;i<r1;i++)
 for(j=0;j<c1;j++)
 {
     printf("\nEnter Element for A[%d][%d] : ",i+1,j+1);
     scanf("%d",&A[i][j]);
 }

//This loop will intake the values of Matrix B

    printf("\nEnter the Elements for Matrix B :\n");
    for(i=0;i<r2;i++)
 for(j=0;j<c2;j++)
 {
     printf("\nEnter Element for B[%d][%d] : ",i+1,j+1);
     scanf("%d",&B[i][j]);
 }

//This loop will Create the Resultant Matrix by initializing all it's Elements as 0

    for(i=0;i<r1;i++)
    {
 for(j=0;j<c2;j++)
 {
     C[i][j]=0;
 }
    }

//This loop starts process of Multiplying 2 Matrix

     for(i=0;i<r1;i++)
    {
 for(j=0;j<c2;j++)
 {   sum=0;
     for(k=0;k<c1;k++)
     {
  sum = sum + A[i][k]*B[k][j];
      }
      C[i][j]=sum;
 }
    }


    printf("\nOutput : \n");
    for(i=0;i<r1;i++)
    {
 for(j=0;j<c2;j++)
     printf("%d\t",C[i][j]);
 printf("\n");
    }

    getch();
    return 0;
}

Output:



Thank-You.

Jai-Hind…!!!

Comments