fork download
  1. #include <stdio.h>
  2. #define DIM 100
  3.  
  4. typedef struct {
  5. int n, m;
  6. int M[DIM][DIM];
  7. } Matrix;
  8.  
  9. int multiply(Matrix A, Matrix B, Matrix *C) {
  10.  
  11. if(A.n - B.m) return 0;
  12.  
  13. for(int i = 0; i < A.n; ++i) {
  14.  
  15. for(int j = 0; j < B.m; ++j) {
  16.  
  17. C->M[ i ][ j ] = 0;
  18. }
  19. }
  20.  
  21. C->n = A.n;
  22. C->m = B.m;
  23.  
  24. for(int i = 0; i < A.n; ++i) {
  25.  
  26. for(int j = 0; j < B.m; ++j) {
  27.  
  28. for(int k = 0; k < A.n; ++k) {
  29.  
  30. C->M[i][j] += A.M[i][k] * B.M[k][j];
  31. }
  32. }
  33. }
  34.  
  35. printf("Matrix Multiplication:\n");
  36. return 1;
  37. }
  38.  
  39. void read(Matrix *mat) {
  40. int n, m;
  41. printf("Lines = ");
  42. scanf("%d", &n);
  43. mat->n = n;
  44. printf("Cols = ");
  45. scanf("%d", &m);
  46. mat->m = m;
  47. for(int i = 0; i < n; ++i) {
  48. for(int j = 0; j < m; ++j) {
  49. printf("mat[%d][%d]=",i,j);
  50. scanf("%d", &mat->M[i][j]);
  51. }
  52. }
  53.  
  54. }
  55.  
  56. void display(Matrix mat) {
  57.  
  58. for(int i = 0; i < mat.n; ++i) {
  59. for(int j = 0; j < mat.m; ++j) {
  60. printf("%d ",mat.M[i][j]);
  61. }
  62. printf("\n");
  63. }
  64. }
  65.  
  66.  
  67. int main(int argc, char const *argv[]) {
  68.  
  69. Matrix A, B, C;
  70. read(&A);
  71. display(A);
  72. read(&B);
  73. display(B);
  74.  
  75. if( multiply(A,B,&C) == 1)
  76.  
  77. display(C);
  78.  
  79. else printf("No Multiplication!");
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 5444KB
stdin
3
3
1 2 3
1 2 3
1 2 3
3
3
1 2 3
1 2 3
1 2 3

stdout
Lines = Cols = mat[0][0]=mat[0][1]=mat[0][2]=mat[1][0]=mat[1][1]=mat[1][2]=mat[2][0]=mat[2][1]=mat[2][2]=1 2 3 
1 2 3 
1 2 3 
Lines = Cols = mat[0][0]=mat[0][1]=mat[0][2]=mat[1][0]=mat[1][1]=mat[1][2]=mat[2][0]=mat[2][1]=mat[2][2]=1 2 3 
1 2 3 
1 2 3 
Matrix Multiplication:
6 12 18 
6 12 18 
6 12 18