C program for Matrix Multiplication

Before going to the program first let is understand what is Matrix Multiplication?

Matrix Multiplication:

Matrix Multiplication is nothing but the multiplication of two matrix to obtain a new matrix.

To perform Matrix Multiplication the number of columns in “matrix 1” must be equal to the number of rows in “matrix 2”.

To multiply a matrix by another matrix we need to do the “dot product” of rows and columns.

Example for Matrix Multiplication:

To get the value of the 1st row and 1st column for new matrix the “dot product” is performed.

matrix-multiply

(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58

We match the 1st members (1 and 7), multiply them, likewise for the 2nd members (2 and 9) and the 3rd members (3 and 11), and finally sum them up.

Similarly other entries of the new matrix are found.

matrix-multiply1

(1, 2, 3) • (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64

(4, 5, 6) • (7, 9, 11) = 4×7 + 5×9 + 6×11 = 139

(4, 5, 6) • (8, 10, 12) = 4×8 + 5×10 + 6×12 = 154

Thus Matrix Multiplication is done and we get the new matrix as given below:

matrix-multiply2

Program code for Matrix  Multiplication:

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[5][5],b[5][5],c[5][5],i,j,k,r1,c1,r2,c2;
    clrscr();
    
    printf("\n  Enter the row and column of matrix 1 :");
    scanf("%d %d",&r1,&c1);
    
    printf("\n  Enter the row and column of matrix 2 :");
    scanf("%d %d",&r2,&c2);
    
    if(c1!=r2)
    {
        printf("\n  Matrix mutiplication is not possible");
        printf("\n\n  Column of 'matrix 1' must be same as row of 'matrix 2'");
    }
    else
    {
        printf("\n  Enter the matrix 1 :\n\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        
        printf("\n  Enter the matrix 2 :\n\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
                scanf("%d",&b[i][j]);
            }
        }
        
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                c[i][j]=0;
                for(k=0;k<c1;k++)
                {
                    c[i][j]=c[i][j]+a[i][k]*b[k][j];
                }
            }
        }
        
        printf("\n  The product matrix is :\n\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                printf("  %d  ",c[i][j]);
            }
            printf("\n");
        }
    }
    getch();
}

Working:

  • First the computer reads the rows and columns of matrix 1 and matrix 2 from the user.
  • Then it compares column 1 and row 2
    • If they are not equal,it prints “matrix multiplication not possible” and exits.
    • Else it continues the program.
  • Then using for loops the computer reads the matrix 1 and matrix 2 from the user.
  • Then using for loops the matrix multiplication is carried out.
  • Finally using for loops the product matrix is printed.

Step by step working of the above program:

  1. Let us assume that a user enters the number of rows and columns of “matrix 1” as 2 and 3 respectively.
  2. And the number of rows and columns of “matrix 2” as 3 and 2 respectively.
  3. Thus r1=2 , c1=3 , r2=3 , c2=2
  4. c1!=r2   (3!=3)    if condition is false

So it goes to the else part.

  1.  And then enters the elements of matrix 1 and matrix 2 as given below

Matrix 1:  2 2 2

   2 2 2

Matrix 2:  3 3

   3 3

   3 3

  1. Then it assigns the value of i=0 , j=0 and k=0 and the loop continues till condition of the for loop is true.

6.1.   i<r1    (0<2)    for loop condition is true

6.1.1.   j<c2    (0<2)    for loop condition is true

 c[i][j]=0    So  c[0][0]=0

6.1.1.1.   k<c1    (0<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[0][0]=c[0][0]+a[0][0]*b[0][0])    So  c[0][0]=6

k++     (k=k+1)    So  k=1

6.1.1.2.   k<c1    (1<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[0][0]=c[0][0]+a[0][1]*b[1][0])    So  c[0][0]=12

k++     (k=k+1)    So  k=2

6.1.1.3.   k<c1    (2<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[0][0]=c[0][0]+a[0][2]*b[2][0])    So  c[0][0]=18

k++    (k=k+1)    So  k=3

6.1.1.4.   k<c1    (3<3)    for loop condition is false

It comes out of the for loop and increment the value of j and reassigns the value of k=0.

j++    (j=j+1)    So  j=1 , k=0

6.1.2.   j<c2    (1<2)    for loop condition is true

 c[i][j]=0    So  c[0][1]=0

6.1.1.1.   k<c1    (0<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[0][1]=c[0][1]+a[0][0]*b[0][1])    So  c[0][1]=6

k++     (k=k+1)    So  k=1

6.1.1.2.   k<c1    (1<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[0][1]=c[0][1]+a[0][1]*b[1][1])    So  c[0][1]=12

k++     (k=k+1)    So  k=2

6.1.1.3.   k<c1    (2<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[0][1]=c[0][1]+a[0][2]*b[2][1])    So  c[0][1]=18

k++    (k=k+1)    So  k=3

6.1.1.4.   k<c1    (3<3)    for loop condition is false

It comes out of the for loop and increment the value of j and reassigns the value of k=0.

j++    (j=j+1)    So  j=2 , k=0

6.1.3.   j<c2    (2<2)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j=0.

i++    (i=i+1)    So  i=1 , j=0

6.2.   i<r1    (1<2)    for loop condition is true

6.2.1.   j<c2    (0<2)    for loop condition is true

 c[i][j]=0    So  c[1][0]=0

6.2.1.1.   k<c1    (0<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[1][0]=c[1][0]+a[1][0]*b[0][0])    So  c[1][0]=6

k++     (k=k+1)    So  k=1

6.2.1.2.   k<c1    (1<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[1][0]=c[1][0]+a[1][1]*b[1][0])    So  c[1][0]=12

k++     (k=k+1)    So  k=2

6.2.1.3.   k<c1    (2<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[1][0]=c[1][0]+a[1][2]*b[2][0])    So  c[1][0]=18

k++    (k=k+1)    So  k=3

6.2.1.4.   k<c1    (3<3)    for loop condition is false

It comes out of the for loop and increment the value of j and reassigns the value of k=0.

j++    (j=j+1)    So  j=1 , k=0

6.2.2.   j<c2    (1<2)    for loop condition is true

 c[i][j]=0    So  c[1][1]=0

6.2.2.1.   k<c1    (0<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[1][1]=c[1][1]+a[1][0]*b[0][1])    So  c[1][1]=6

k++     (k=k+1)    So  k=1

6.2.2.2.   k<c1    (1<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[1][1]=c[1][1]+a[1][1]*b[1][1])    So  c[1][1]=12

k++     (k=k+1)    So  k=2

6.2.2.3.   k<c1    (2<3)    for loop condition is true

c[i][j]=c[i][j]+a[i][k]*b[k][j]     (c[1][1]=c[1][1]+a[1][2]*b[2][1])    So  c[1][1]=18

k++    (k=k+1)    So  k=3

6.2.2.4.   k<c1    (3<3)    for loop condition is false

It comes out of the for loop and increment the value of j and reassigns the value of k=0.

j++    (j=j+1)    So  j=2 , k=0

6.2.3.   j<c2    (2<2)    for loop condition is false

It comes out of the for loop and increment the value of i and reassigns the value of j=0.

i++    (i=i+1)    So  i=1 , j=0

6.3.   i<r1    (2<2)    for loop condition is false

It comes out of the for loop.

  1. Finally using two for loops it prints the elements of the Product matrix as given below:

Product matrix:   18  18

 18  18

  1. Thus the program execution is completed.

Output:

matrix multiplication

matrix multiplication

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *