Advertisement
Infidelis

Matrixes Multiply V2

Mar 27th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. typedef struct matrix {
  6.     float ** data;
  7.     int stroke;
  8.     int colum;
  9. } matrix;
  10.  
  11. typedef struct mult {
  12.     matrix A;
  13.     matrix B;
  14.     matrix C;
  15. } mult;
  16.  
  17.  
  18. void memory(mult *c)
  19. {
  20.     c->C.stroke = c->A.stroke;
  21.     c->C.colum = c->B.colum;
  22.     c->A.data = (float**)malloc(c->A.stroke * sizeof(matrix));
  23.     c->B.data = (float**)malloc(c->B.stroke * sizeof(matrix));
  24.     c->C.data = (float**)malloc(c->C.stroke * sizeof(matrix));
  25.     for (int i = 0; i < c->A.stroke; i++)
  26.     {
  27.         c->A.data[i] = (float*)malloc(c->A.colum * sizeof(matrix));
  28.         c->C.data[i] = (float*)malloc(c->C.colum * sizeof(matrix));
  29.     }
  30.     for (int i = 0; i < c->B.stroke; i++)
  31.     {
  32.         c->B.data[i] = (float*)malloc(c->B.colum * sizeof(matrix));
  33.     }
  34. }
  35. int read(mult *c)
  36. {
  37.     printf_s("Matrix A(n x k): ");
  38.     scanf_s("%d %d", &c->A.stroke, &c->A.colum);
  39.     printf_s("Matrix B(k x m): ");
  40.     scanf_s("%d %d", &c->B.stroke, &c->B.colum);
  41.     if (c->B.stroke != c->A.colum) { printf_s("Can't multiply!");  return 1; }
  42.     memory(c);
  43.     printf_s("Matrix A\n");
  44.     for (int i = 0; i < c->A.stroke; i++)
  45.     {
  46.         printf_s("S%d: ", i);
  47.         for (int j = 0; j < c->A.colum; j++) { scanf_s("%f", &c->A.data[i][j]); }
  48.     }
  49.     printf_s("Matrix B\n");
  50.     for (int i = 0; i < c->B.stroke; i++)
  51.     {
  52.         printf_s("S%d: ", i);
  53.         for (int j = 0; j < c->B.colum; j++) { scanf_s("%f", &c->B.data[i][j]); }
  54.     }
  55.     for (int i = 0; i < c->C.stroke; i++)
  56.     {
  57.         for (int j = 0; j < c->C.colum; j++) { c->C.data[i][j] = 0; }
  58.     }
  59.     return 0;
  60. }
  61.  
  62. void multiply(mult *c)
  63. {
  64.     for (int i = 0; i < c->C.stroke; i++)
  65.     {
  66.         for (int j = 0; j < c->C.colum; j++)
  67.         {
  68.             for (int k = 0; k < c->A.colum; k++) { c->C.data[i][j] += c->A.data[i][k] * c->B.data[k][j]; }
  69.         }
  70.     }
  71. }
  72.  
  73. void output(mult *c)
  74. {
  75.     printf_s("Matrix C(n x m): \n");
  76.     for (int i = 0; i < c->C.stroke; i++)
  77.     {
  78.         for (int j = 0; j < c->C.colum; j++) { printf_s("%.1f ", c->C.data[i][j]); }
  79.         printf_s("\n");
  80.     }
  81. }
  82.  
  83. void memory_free(mult *c)
  84. {
  85.     for (int i = 0; i < c->B.stroke; i++) { free(c->B.data[i]); }
  86.     for (int i = 0; i < c->A.stroke; i++)
  87.     {
  88.         free(c->A.data[i]);
  89.         free(c->C.data[i]);
  90.     }
  91.     free(c->A.data);
  92.     free(c->B.data);
  93.     free(c->C.data);
  94.     free(c);
  95. }
  96.  
  97. int main()
  98. {
  99.     mult *c = (mult*)malloc(sizeof(mult));
  100.     if (read(c)) {
  101.         _getch();
  102.         return 0; }
  103.     multiply(c);
  104.     output(c);
  105.     memory_free(c);
  106.     _getch();
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement