fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. //фуникция выделения памяти для матриц
  7. int **MatCreate(int stroki,int stolbci){
  8. int **mat = new int * [stroki];
  9. for (int i = 0; i < stroki; ++i){
  10. mat[i] = new int [stolbci];}
  11. return mat;};
  12. //функция заполнения матриц
  13. void MatIn(int stroki,int stolbci,int **mat){
  14. for (int i = 0; i < stroki; ++i){
  15. for (int j = 0; j < stolbci; ++j){
  16. cin >> mat[i][j];}}
  17. };
  18. //функция обнуления значений в матрице произведения
  19. void ObnulatorSum(int **mat3,int strokimat1, int stolbcimat2){
  20. for (int i = 0; i < strokimat1; ++i){
  21. for (int j = 0; j < stolbcimat2; ++j){
  22. mat3[i][j] = 0;
  23. }}
  24. }
  25. //функция подсчета матрицы произведения
  26. void MatUm(int **mat1,int **mat2,int **mat3,int a,int d,int b){
  27. for (int i = 0; i < a; ++i)
  28. for (int n = 0; n < d; ++n)
  29. for (int j = 0; j < b; ++j)
  30. mat3[i][n] += mat1[i][j] * mat2[j][n];
  31. }
  32. //функция выводящая значения матриц
  33. void matprint(int **mat, int strokimat, int stolbcimat){
  34. for (int i = 0; i < strokimat; ++i){
  35. for (int n = 0; n < stolbcimat; ++n){
  36. cout << setw(3) <<mat[i][n];
  37. }
  38. cout<<endl;
  39. }
  40. };
  41. //функция освобождения памяти
  42. void matdelete(int **mat, int stroki){
  43. for (int i = 0; i < stroki; ++i) {
  44. delete [] mat[i];
  45. }
  46. delete [] mat;
  47. }
  48.  
  49.  
  50.  
  51. int main() {
  52. int a ,b ,c ,d;
  53. int **mat1,**mat2,**mat3;
  54. cin >> a >> b >> c >> d;//ввод строк и столбцов первой и второй матрицы
  55. if (b == c){
  56.  
  57. mat1 = MatCreate(a,b);
  58. mat2 = MatCreate(c,d);
  59. mat3 = MatCreate(a,d);
  60.  
  61. MatIn(a,b,mat1);
  62. cout << "матрица №1"<<endl ;
  63. matprint(mat1,a,b);
  64.  
  65. MatIn(c,d,mat2);
  66. cout << "матрица №2"<<endl ;
  67. matprint(mat2,c,d);
  68.  
  69. ObnulatorSum(mat3,a,d);
  70. cout << "матрица №3"<<endl ;
  71. matprint(mat3,a,d);
  72. cout << "матрица произведения: "<< a <<" x "<<d <<endl;
  73. MatUm(mat1, mat2, mat3, a,d,b); /*я не могу объяснить феномен почему у меня матрица
  74. считается нормально а выводиться ненормально */
  75.  
  76. matprint(mat3,a,d);
  77.  
  78. matdelete(mat1,a);
  79. matdelete(mat2,c);
  80. matdelete(mat3,a);
  81.  
  82. }
  83. else{
  84. cout << " матрицы не совместимы " << endl;
  85. }
  86.  
  87. return 0;
  88. }
Success #stdin #stdout 0s 4404KB
stdin
3 2 2 4
1 1
2 2
3 3
3 2 1 3
3 2 1 2
stdout
матрица №1
  1  1
  2  2
  3  3
матрица №2
  3  2  1  3
  3  2  1  2
матрица №3
  0  0  0  0
  0  0  0  0
  0  0  0  0
матрица произведения: 3 x 4
  6  4  2  5
 12  8  4 10
 18 12  6 15