SStive39

OOP_k2l1v8

Sep 26th, 2021
667
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. int determinant(T matrix, int size) {
  7.     int mult1 = 1, mult2 = 1;
  8.    
  9.     for (int i = 0; i < size; i++)
  10.         mult1 *= matrix[i][i];
  11.    
  12.     for (int i = 0; i < size; i++)
  13.         mult2 *= matrix[i][size - 1 - i];
  14.    
  15.     return mult1 - mult2;
  16. };
  17.  
  18. template <typename T>
  19. T** generate_matrix(int size) {
  20.     T** matr = new T*[size];
  21.     for (int i = 0; i < size; i++) {
  22.         matr[i] = new T[size];
  23.         for (int j = 0; j < size; j++) {
  24.             matr[i][j] = rand() % 10 + 0;
  25.             cout << matr[i][j] << " ";
  26.         }
  27.         cout << endl;
  28.     }
  29.     return matr;
  30. };
  31.  
  32. int main() {
  33.     cout << "Matrix A type int, size 3x3:" << endl;
  34.     int** A = generate_matrix<int>(3);
  35.     cout << "Determinant A = " << determinant(A, 3) << endl;
  36.    
  37.     cout << endl;
  38.    
  39.     cout << "Matrix B type float, size 5x5:" << endl;
  40.     float** B = generate_matrix<float>(5);
  41.     cout << "Determinant B = " << determinant(B, 5) << endl;
  42.    
  43.     return 0;
  44. }
RAW Paste Data