Advertisement
dmilicev

multiplication of matrices v1.c

Oct 11th, 2019
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. /*
  2.     multiplication of matrices v1.c
  3.  
  4.     https://en.wikipedia.org/wiki/Matrix_(mathematics)#Matrix_multiplication
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. // display mask of matrix elements
  11. void MatrixMask(char text[], int rows, int columns)
  12. {
  13.     int r,c;
  14.  
  15.     printf("\n\n Matrix %s [%d x %d] is: \n\n", text, rows, columns);
  16.  
  17.     for(r = 1; r <= rows; r++)          // for all rows, one by one
  18.     {
  19.         for(c = 1; c <= columns; c++)   // print row
  20.         {
  21.             printf("  %s[%d%d]  ", text, r, c);
  22.         }
  23.  
  24.         printf("\n\n");                 // new row
  25.     }
  26. }
  27.  
  28. // display matrix elements
  29. void displayMatrix(char text[], float Matrix[][10], int rows, int columns)
  30. {
  31.     int i, j;
  32.  
  33.     printf("%s",text);
  34.  
  35.     for(i = 0; i < rows; i++)
  36.     {
  37.         for(j = 0; j < columns; j++)
  38.         {
  39.             printf(" %10.2f ", Matrix[i][j]);
  40.         }
  41.  
  42.         printf("\n\n");                 // new row
  43.     }
  44. }
  45.  
  46. // input matrix elements
  47. void enterMatrix(char text[], float Matrix[][10], int rows, int columns)
  48. {
  49.     int i, j;
  50.  
  51.     printf("%s",text);
  52.  
  53.     for(i = 0; i < rows; i++)
  54.     {
  55.         for(j = 0; j < columns; j++)
  56.         {
  57.             printf("\n Enter element [%d%d] : ", i+1, j+1);
  58.             scanf("%f", &Matrix[i][j]);
  59.         }
  60.     }
  61. }
  62.  
  63. // multiplay two matrices, Matrix1[rows1,colums1] and Matrix2[rows2,columns2]
  64. // result is stored in resultMatrix[rows1,columns2]
  65. void multiplyMatrices(float Matrix1[][10], float Matrix2[][10], float resultMatrix[][10],
  66.                       int rows1, int columns1, int rows2, int columns2)
  67. {
  68.     int i, j, k;
  69.  
  70.     // Initializing elements of matrix resultMatrix to 0.
  71.     for(i = 0; i < rows1; i++)
  72.     {
  73.         for(j = 0; j < columns2; j++)
  74.         {
  75.             resultMatrix[i][j] = 0;
  76.         }
  77.     }
  78.  
  79.     // Multiplying matrix Matrix1 and Matrix2 and storing in array resultMatrix
  80.     for(i = 0; i < rows1; i++)
  81.     {
  82.         for(j = 0; j < columns2; j++)
  83.         {
  84.             for(k=0; k<columns1; k++)
  85.             {
  86.                 resultMatrix[i][j] += Matrix1[i][k] * Matrix2[k][j];
  87.             }
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93. int main(void)
  94. {
  95.     float Matrix1[10][10], Matrix2[10][10], resultMatrix[10][10];
  96.     int rows1, columns1, rows2, columns2;
  97.  
  98.     // Input numbers of rows and colums of matrices
  99.     // If columns of first matrix is not equal to rows of second matrix,
  100.     // asking user to enter the size of matrices again
  101.     do
  102.     {
  103.         printf("\n \t Matrix multiplication \n");
  104.         printf("\n Columns of first matrix must be equal to rows of second matrix !\n");
  105.         printf("\n columns1 must be equal to rows2 ! \n\n");
  106.  
  107.         printf("\n Enter number of rows for first matrix:        rows1 = ");
  108.         scanf("%d", &rows1);
  109.  
  110.         printf("\n Enter number of columns for first matrix:  columns1 = ");
  111.         scanf("%d", &columns1);
  112.  
  113.  
  114.         printf("\n Enter number of rows for second matrix:       rows2 = ");
  115.         scanf("%d", &rows2);
  116.  
  117.         printf("\n Enter number of columns for second matrix: columns2 = ");
  118.         scanf("%d", &columns2);
  119.  
  120.     } while (columns1 != rows2);
  121.  
  122.     // enter matrices data
  123.     MatrixMask("Matrix1", rows1, columns1);
  124.     enterMatrix("\n Enter elements of Matrix1 \n", Matrix1, rows1, columns1);
  125.  
  126.     MatrixMask("Matrix2", rows2, columns2);
  127.     enterMatrix("\n Enter elements of Matrix2 \n", Matrix2, rows2, columns2);
  128.  
  129.     // multiply two matrices
  130.     multiplyMatrices(Matrix1, Matrix2, resultMatrix, rows1, columns1, rows2, columns2);
  131.  
  132.     // display matrices
  133.     displayMatrix("\n Matrix1 is: \n\n", Matrix1, rows1, columns1);
  134.     displayMatrix("\n Matrix2 is: \n\n", Matrix2, rows2, columns2);
  135.     displayMatrix("\n resultMatrix is: \n\n", resultMatrix, rows1, columns2);
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement