Search Program on this blog

Friday 28 August 2015

C program to multiply 2 matrices

To Develop the logic and trace the indices in 2-D array workout small problem on paper and observe the indices


/*
    c Program to multiply two matrices
*/


#include<stdio.h>
#include<stdlib.h>
int main()
{
        int A[3][3]={   {1,2,3},
                        {4,5,6},
                        {7,8,9}
                     };

        int B[3][3]={   {1,2,3},
                        {4,5,6},
                        {7,8,9}
                    };

        int i,j,k,l,temp=0;
        int C[3][3];    //result matrix

        for(i=0;i<3;i++){
                for(j=0;j<3;j++){
                        temp=0;
                        for(k=0;k<3;k++){
                                temp=temp+A[i][k]*B[k][j];
                        }
                        C[i][j]=temp;
                }
        }
        //To print the resultant matrix
        for(i=0;i<3;i++){
                for(j=0;j<3;j++){
                        printf("%d ",C[i][j]);
                }
                printf("\n");
        }

        return 0;
}

No comments:

Post a Comment