Industrial Training




2 Dimensional matrix multiplication


Let we have two 2 x 2 matrices A and B
Let we have two 2 x 2 matrices A and B


A=[a00a10a01a11] and B=[b00b10b01b11]


The multiplication of A and B works as follows

  • Multiply a00 with b00 and a01 with b10 and sum them together. So the first element r00 becomes a00 . b00 + a01 . b10
  • Multiply a00 with b01 and a01 with b11 and sum them together. So the first element r01 becomes a00 . b01 + a01 . b11

  • Multiply a10 with b00 and a11 with b10 and sum them together. So the first element r10 becomes a10 . b00 + a11 . b10

  • Multiply a10 with b01 and a11 with b11 and sum them together. So the first element r11 becomes a10 . b01 + a11 . b11

So the resulting matrix R becomes,


R=[a00.b00+a01.b10a10.b00+a11.b10a00.b01+a01.b11a10.b01+a11.b11]


Note: In order to multiply two matrices, A and B, the number of columns in A must equal the number of rows in B. Thus, if A is an m∗n matrix and B is an r∗s matrix, n=r.


Source Code:


#include< stdio.h>
int main()
{
    int r1, c1, r2, c2, matrix1[10][10], matrix2[10][10], result[10][10];
    int i, j, k;
    printf("Enter the row and column of the first matrix: ");
    scanf("%d%d",&r1,&c1);
    printf("Enter the row and column of the second matrix: ");
    scanf("%d%d",&r2,&c2);
    if(c1 != r2){
        printf("Matrix multiplication impossible");
    }
    printf("Enter the first matrix: \n");
    for(i = 0; i < r1; i++)
       for(j = 0; j < c1; j++)
            scanf("%d", &matrix1[i][j]);
    printf("Enter the second matrix: \n");
    for(i = 0; i < r2; i++)
       for(j = 0; j < c2; j++)
            scanf("%d", &matrix2[i][j]);
    for(i = 0; i < r1; i++ ){
        for(j = 0; j < c2; j++){
            result[i][j] = 0;
            for(k = 0; k < c1; k++){
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    printf("The multiplication of the matrix is: \n");
    for(i = 0; i < r1; i++){
        for(j = 0; j < c2; j++){
            printf("%d", result[i][j]);
            printf(" ");
        }
        printf("\n");
    }
    return 0;
}


Hi I am Pluto.