Matrix multiplication using C

 

Hey guys once again welcome to the Decoders blog, I hope you all are doing well and today I'm present with another logic but this time with something different it is a C logic which will help us to get the multiplication of given two matrices using arrays in C 

Here's the code which you can go through...........

//Matrix multiplication
#include<stdio.h>
int main()
{
    int a[3][3],b[3][3],c[3][3];
    int i,j,k;
    printf("Enter the value of the matrix a:\n");
    //Storing the values for the matrix [A]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter the value of the matrix a:\n");
    //Storing the values for the matrix [B]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }

    //Displaying the matrix [A]
    printf("Matrix A:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",a[i][j]);
        }
        printf("\n");
    }

    //Displaying the matrix [B]
    printf("Matrix B:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",b[i][j]);
        }
        printf("\n");
    }

    //Logic for storing the values of the matrix [C]
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            c[i][j]=0;
            for(k=0;k<3;k++)
            {
                c[i][j] += a[i][k]*b[k][j];
            }
        }
    }

    //Displaying the values of the matrix [C]
    printf("Matrix C:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",c[i][j]);
        }
        printf("\n");
    }
    return(0);
}

Comments

Popular Posts