rootUser

Matrix multiplication

May 26th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*C programme to multiply mattrix*/
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
  6.     printf("Enter rows and column for first matrix: ");
  7.     scanf("%d%d", &r1, &c1);
  8.     printf("Enter rows and column for second matrix: ");
  9.     scanf("%d%d",&r2, &c2);
  10.  
  11. /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
  12.     while (c1!=r2)
  13.     {
  14.         printf("Error! column of first matrix not equal to row of second.\n\n");
  15.         printf("Enter rows and column for first matrix: ");
  16.         scanf("%d%d", &r1, &c1);
  17.         printf("Enter rows and column for second matrix: ");
  18.         scanf("%d%d",&r2, &c2);
  19.     }
  20.  
  21. /* Storing elements of first matrix. */
  22.     printf("\nEnter elements of matrix 1:\n");
  23.     for(i=0; i<r1; ++i)
  24.     for(j=0; j<c1; ++j)
  25.     {
  26.         printf("Enter elements a%d%d: ",i+1,j+1);
  27.         scanf("%d",&a[i][j]);
  28.     }
  29.  
  30. /* Storing elements of second matrix. */
  31.     printf("\nEnter elements of matrix 2:\n");
  32.     for(i=0; i<r2; ++i)
  33.     for(j=0; j<c2; ++j)
  34.     {
  35.         printf("Enter elements b%d%d: ",i+1,j+1);
  36.         scanf("%d",&b[i][j]);
  37.     }
  38.  
  39. /* Initializing elements of matrix mult to 0.*/
  40.     for(i=0; i<r1; ++i)
  41.     for(j=0; j<c2; ++j)
  42.     {
  43.        mult[i][j]=0;
  44.     }
  45.  
  46. /* Multiplying matrix a and b and storing in array mult. */
  47.     for(i=0; i<r1; ++i)
  48.     for(j=0; j<c2; ++j)
  49.     for(k=0; k<c1; ++k)
  50.     {
  51.         mult[i][j]+=a[i][k]*b[k][j];
  52.     }
  53.  
  54. /* Displaying the multiplication of two matrix. */
  55.     printf("\nOutput Matrix:\n");
  56.     for(i=0; i<r1; ++i)
  57.     for(j=0; j<c2; ++j)
  58.     {
  59.         printf("%d  ",mult[i][j]);
  60.         if(j==c2-1)
  61.             printf("\n\n");
  62.     }
  63.     return 0;
  64. }
Add Comment
Please, Sign In to add comment