Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday 3 October 2013

Matrix multiplication program in C language

This tutorial teaches about Matrix Multiplication. Matrix is an array of rows and columns. Matrix Multiplication is a binary operation and finally generates new matrix.
Matrix Multiplication Program In C Language
Matrix Multiplication ~ All Code Tips

NOTE: Number of columns of first matrix is must be equal to number of rows of second matrix.


 #include<stdio.h>  
 int main()  
 {  
   int m, n, p, q, i, j, k, sum = 0;  
   int matrix1[10][10], matrix2[10][10], matrix_mul[10][10];  
   printf("Enter No of rows and columns of matrix-1");  
   scanf("%d%d", &m, &n);  
   printf("Enter elements of matrix-1 : ");  
   for(i = 0; i < m; i++)  
     for(j = 0; j < n; j++)  
       scanf("%d", &matrix1[i][j]);  
   printf("Enter No of rows and columns of matrix-2");  
   scanf("%d%d", &p, &q);  
   if ( n != p )  
     printf("Matrix multiplication not possible");  
   else  
   {  
     printf("Enter elements of matrix-2 : ");  
     for(i = 0; i < p; i++)  
       for(j = 0; j < q; j++)  
         scanf("%d", &matrix2[i][j]);  
     for(i = 0; i < m; i++)  
     {  
       for(j = 0; j < q; j++)  
       {  
         for(k = 0; k < p; k++)  
         {  
           sum = sum + matrix1[i][k] * matrix2[k][j];  
         }  
         matrix_mul[i][j] = sum;  
         sum = 0;  
       }  
     }  
     printf("Product of Matrix-1 and Matrix-2 : \n");  
     for(i = 0; i < m; i++)  
     {  
       for(j = 0; j < q; j++)  
         printf("%d\t", matrix_mul[i][j]);  
       printf("\n");  
     }  
   }  
   return 0;  
 }  

0 comments :

Post a Comment