fork download
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <iomanip>
  7. using namespace std;
  8. class Matx_Op
  9. {
  10. int row, col, i, j;
  11. float k;
  12. public :
  13. void Scalar_Mulp()
  14. {
  15. cin >> k; //Input scalar
  16. cin >> row >> col; //Input dimension
  17. float A[row][col];
  18. for (i=0; i<row; i++)
  19. {
  20. for (j=0; j<col; j++)
  21. {
  22. cin >> A[i][j] ; //Input matrix
  23. }
  24. }
  25. for (i=0; i<row; i++)
  26. {
  27. for (j=0; j<col; j++)
  28. {
  29. cout << fixed << setprecision(1) << k*A[i][j] << " " ; //Output resultant matrix
  30. }
  31. cout << endl;
  32. }
  33. }
  34.  
  35. void matx_mulp()
  36. {
  37. int p,q,r,s;
  38. cin >> p >> q; //Input dimensions of first matrix
  39. int A[p][q];
  40. for (i=0; i<p; i++)
  41. {
  42. for (j=0; j<q; j++)
  43. {
  44. cin >> A[i][j] ; //Input matrix
  45. }
  46. }
  47. cin >> r >> s; //Input dimensions of second matrix
  48. int B[r][s];
  49. for (i=0; i<r; i++)
  50. {
  51. for (j=0; j<s; j++)
  52. {
  53. cin >> B[i][j] ; //Input matrix
  54. }
  55. }
  56. if(q==r)
  57. {
  58. int C[p][s];
  59. for (i=0; i<p; i++)
  60. {
  61. for (j=0; j<s; j++)
  62. {
  63. C[i][j] = 0;
  64. for (int d=0; d<q; d++)
  65. {
  66. C[i][j] += A[i][d]*B[d][j] ;
  67. }
  68. }
  69. }
  70. for (i=0; i<p; i++)
  71. {
  72. for (j=0; j<s; j++)
  73. {
  74. cout << C[i][j] << " " ; //Output resultant matrix
  75. }
  76. cout << endl;
  77. }
  78. }
  79. }
  80.  
  81. void matx_add()
  82. {
  83. int p,q,r,s;
  84. cin >> p >> q; //Input dimensions of first matrix
  85. int A[p][q];
  86. for (i=0; i<p; i++)
  87. {
  88. for (j=0; j<q; j++)
  89. {
  90. cin >> A[i][j] ; //Input matrix
  91. }
  92. }
  93. cin >> r >> s; //Input dimensions of second matrix
  94. int B[r][s];
  95. for (i=0; i<r; i++)
  96. {
  97. for (j=0; j<s; j++)
  98. {
  99. cin >> B[i][j] ; //Input matrix
  100. }
  101. }
  102.  
  103. if(p==r && q==s)
  104. {
  105. int C[p][r];
  106. for (i=0; i<p; i++)
  107. {
  108. for (j=0; j<r; j++)
  109. {
  110. C[i][j] = A[i][j] + B[i][j];
  111. }
  112. }
  113.  
  114. for (i=0; i<p; i++)
  115. {
  116. for (j=0; j<r; j++)
  117. {
  118. cout << C[i][j] << " " ;
  119. }
  120. cout << endl;
  121. }
  122. }
  123. }
  124.  
  125. void conc()
  126. {
  127. int num, k, i, j, r=0, c=0;
  128. cin >> num ; //Input number of matrices
  129. for (k=1; k<=num; k++)
  130. {
  131. cin >> row >> col;
  132. int A[row][col];
  133. for (i=0; i<row; i++)
  134. {
  135. for (j=0; j<col; j++)
  136. {
  137. cin >> A[i][j];
  138. }
  139. }
  140.  
  141. int C[50][50];
  142. for (int r; r<row; r++)
  143. {
  144. for (int c; c<col; c++)
  145. {
  146. C[r][c] = A[r][c];
  147. }
  148. }
  149. r= row;
  150. c= col;
  151.  
  152.  
  153.  
  154.  
  155. for (i=0; i<row; i++)
  156. {
  157. for (j=0; j<col; j++)
  158. {
  159. cout << A[i][j] << " " ;
  160. }
  161. cout << endl;
  162. }
  163.  
  164. }
  165. }
  166. };
  167.  
  168. int main()
  169. {
  170. Matx_Op Matrix; // Operation to be performed..
  171. int check;
  172. cin >> check; //Input function to be performed
  173.  
  174. if (check==1)
  175. {
  176. Matrix.Scalar_Mulp();
  177. }
  178. else if (check==2)
  179. {
  180. Matrix.matx_mulp() ;
  181. }
  182. else if (check==3)
  183. {
  184. Matrix.matx_add() ;
  185. }
  186. else
  187. {
  188. Matrix.conc() ;
  189. }
  190. return 0;
  191. }
  192.  
Success #stdin #stdout 0s 4516KB
stdin
1
2
3
3
2
2
2
2
2
2
2
2
2
stdout
4.0 4.0 4.0 
4.0 4.0 4.0 
4.0 4.0 4.0