fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7. int a[2][2] = {{1, 2},
  8. {3, 4}};
  9. int b[2][2] = {{5, 6},
  10. {7, 8}};
  11. int mul[2][2];
  12. int sum[2][2];
  13.  
  14. //sum
  15. for (int i = 0; i < 2; i++) {
  16. for (int j = 0; j < 2; j++) {
  17. sum[i][j] = a[i][j]+b[i][j];
  18. }
  19. }
  20. //print summ
  21. cout<<"matrix addition: \n";
  22. for (int i = 0; i < 2; i++) {
  23. for (int j = 0; j < 2; j++) {
  24. cout << sum[i][j] << " ";
  25. }
  26. cout << endl;
  27. }
  28.  
  29. for (int i = 0; i < 2; i++) {
  30. for (int j = 0; j < 2; j++) {
  31. mul[i][j] = 0;
  32. for (int k = 0; k < 2; k++) {
  33. mul[i][k] += a[i][j] * b[j][k];
  34. }
  35. }
  36. }
  37.  
  38. //print mul
  39. cout<<"matrix multiplication: \n";
  40.  
  41. for (int i = 0; i < 2; i++) {
  42. for (int j = 0; j < 2; j++) {
  43. cout << mul[i][j] << " ";
  44. }
  45. cout << endl;
  46. }
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
matrix addition: 
6    8    
10    12    
matrix multiplication: 
19    16    
43    32