rootUser

MULTYPLY 2 MATTRIXS

May 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* C programme to multiple two mattrixs */
  2. #include <stdio.h>
  3. int main()
  4. {
  5.   int m, n, p, q, c, d, k, sum = 0;
  6.   int first[10][10], second[10][10], multiply[10][10];
  7.  
  8.   printf("Enter the number of rows and columns of first matrix\n");
  9.   scanf("%d%d", &m, &n);
  10.   printf("Enter the elements of first matrix\n");
  11.  
  12.   for (  c = 0 ; c < m ; c++ )
  13.     for ( d = 0 ; d < n ; d++ )
  14.       scanf("%d", &first[c][d]);
  15.  
  16.   printf("Enter the number of rows and columns of second matrix\n");
  17.   scanf("%d%d", &p, &q);
  18.  
  19.   if ( n != p )
  20.     printf("Matrices with entered orders can't be multiplied with each other.\n");
  21.   else
  22.   {
  23.     printf("Enter the elements of second matrix\n");
  24.  
  25.     for ( c = 0 ; c < p ; c++ )
  26.       for ( d = 0 ; d < q ; d++ )
  27.         scanf("%d", &second[c][d]);
  28.  
  29.     for ( c = 0 ; c < m ; c++ )
  30.     {
  31.       for ( d = 0 ; d < q ; d++ )
  32.       {
  33.         for ( k = 0 ; k < p ; k++ )
  34.         {
  35.           sum = sum + first[c][k]*second[k][d];
  36.         }
  37.  
  38.         multiply[c][d] = sum;
  39.         sum = 0;
  40.       }
  41.     }
  42.  
  43.     printf("Product of entered matrices:-\n");
  44.  
  45.     for ( c = 0 ; c < m ; c++ )
  46.     {
  47.       for ( d = 0 ; d < q ; d++ )
  48.         printf("%d\t", multiply[c][d]);
  49.  
  50.       printf("\n");
  51.     }
  52.   }
  53.   return 0;
  54. }
Add Comment
Please, Sign In to add comment