fork download
  1. #include<stdio.h>
  2.  
  3. #define N (2)
  4.  
  5. void mult(double *ma, double *mb, double *mc, int n)
  6. {
  7. int i, j, k;
  8. double sum;
  9. for (j = 0; j < n; j++) {
  10. for (i = 0; i < n; i++) {
  11. sum = 0.0;
  12. for (k = 0; k < n; k++) {
  13. sum += *(ma + j * n + k) * (*(mb + k * n + i));
  14. }
  15. *(mc + j * n + i) = sum;
  16. }
  17. }
  18. }
  19.  
  20. void print_matrix(double *x, char *s)
  21. {
  22. int i, j;
  23. printf("%s =\n", s);
  24. for (j = 0; j < N; j++) {
  25. printf(" | ");
  26. for (i = 0; i < N; i++) {
  27. printf("%f ", *(x + j * N + i));
  28. }
  29. printf("|\n");
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. double ma[N][N] = { {1.0, 2.0}, {3.0, 4.0} };
  36. double mb[N][N] = { {1.0, 2.0}, {-2.0, -1.0} };
  37. double mc[N][N] = { 0.0 };
  38.  
  39. mult((double *) ma, (double *) mb, (double *) mc, N);
  40. print_matrix((double *) ma, "ma");
  41. print_matrix((double *) mb, "mb");
  42. print_matrix((double *) mc, "mc = ma * mb");
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
ma =
  | 1.000000 2.000000 |
  | 3.000000 4.000000 |
mb =
  | 1.000000 2.000000 |
  | -2.000000 -1.000000 |
mc = ma * mb =
  | -3.000000 0.000000 |
  | -5.000000 2.000000 |