fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void pp(int *a, int r, int c,const char *msg)
  5. {
  6. int i,j;
  7. printf("%s\n", msg);
  8. for ( i = 0; i < r; ++i) {
  9. for ( j = 0; j < c; ++j) {
  10. printf("%4d", *a++);
  11. }
  12. puts("");
  13. }
  14. puts("");
  15. }
  16.  
  17. int
  18. main()
  19. {
  20. int a[][2] = {
  21. {1, 2},
  22. {3, 4},
  23. {5, 6}
  24. };
  25. int b[][3] = {
  26. {1, 2, 4},
  27. {3, 6, 9}
  28. };
  29.  
  30. int row = sizeof(a) / sizeof(a[0]);
  31. int col = sizeof(a[0]) / sizeof(int);
  32. int *c = (int *)malloc(sizeof(int) * row * col);
  33. int i;
  34. for (i = 0; i < row * row; ++i) {
  35. int w1 = i / row * col, w2 = i % row;
  36. c[i] += *(*a + w1) * *(*b + w2);
  37. w1 += col;
  38. w2 += row;
  39. c[i] += *(*a + w1) * *(*b + w2);
  40. }
  41.  
  42. pp(*a,3,2,"行列xを表示");
  43. pp(*b,2,3,"行列yを表示");
  44. pp(c,3,3,"行列xとyの積を表示");
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0.02s 1852KB
stdin
Standard input is empty
stdout
行列xを表示
   1   2
   3   4
   5   6

行列yを表示
   1   2   4
   3   6   9

行列xとyの積を表示
  10  20  31
  18  36  57
10717541492143643435-1079704844