fork download
  1. #include <stdio.h>
  2.  
  3. //2x3行列の表示
  4. void printMatrix(int mt[][3])
  5. {
  6. int i,j;
  7.  
  8. for(i = 0;i < 2;i++) {
  9. for(j = 0;j < 3;j++) {
  10. printf("%d ", mt[i][j]);
  11. }
  12. printf("\n");
  13. }
  14. }
  15.  
  16. //2x3行列の加算
  17. //mt1とmt2の加算結果をmtAnsに格納する
  18. void addMatrix(int mtAns[][3], int mt1[][3], int mt2[][3])
  19. {
  20. int i,j;
  21.  
  22. for(i = 0;i < 2;i++) {
  23. for(j = 0;j < 3;j++) {
  24. mtAns[i][j] = mt1[i][j] + mt2[i][j];
  25. }
  26. }
  27. }
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31. int matrix1[][3] = {
  32. { 2, 3, 4 },
  33. { 5, 6, 7 },
  34. };
  35. int matrix2[2][3] = {
  36. { 7, 6, 5 },
  37. { 4, 3, 2 },
  38. };
  39. int matrixAns[][3] = {
  40. { 0, 0, 0 },
  41. { 0, 0, 0 },
  42. };
  43.  
  44. printf("matrix1\n");
  45. printMatrix(matrix1);
  46. printf("\nmatrix2\n");
  47. printMatrix(matrix2);
  48. printf("\n\n");
  49.  
  50. addMatrix(matrixAns, matrix1, matrix2);
  51.  
  52. printf("Matrix add\n");
  53. printMatrix(matrixAns);
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
matrix1
2 3 4 
5 6 7 

matrix2
7 6 5 
4 3 2 


Matrix add
9 9 9 
9 9 9