Advertisement
Guest User

Untitled

a guest
May 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4. int determinant( int matrix[10][10], int n) {
  5. int det = 0;
  6. int submatrix[10][10];
  7. if (n == 2)
  8. return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
  9. else {
  10. for (int x = 0; x < n; x++) {
  11. int subi = 0;
  12. for (int i = 1; i < n; i++) {
  13. int subj = 0;
  14. for (int j = 0; j < n; j++) {
  15. if (j == x)
  16. continue;
  17. submatrix[subi][subj] = matrix[i][j];
  18. subj++;
  19. }
  20. subi++;
  21. }
  22. /******************************************************************************
  23. cout<<"Submatrix is:"<<endl;
  24. for (int i = 0; i < n-1; i++) {
  25. for (int j = 0; j < n-1; j++)
  26. cout << submatrix[i][j] <<" ";
  27. cout<<endl;
  28. }
  29. *******************************************************************************/
  30. det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ));
  31. }
  32. }
  33. return det;
  34. }
  35. int main() {
  36. int n, i, j;
  37. int matrix[10][10];
  38. cout << "Enter the size of the matrix : ";
  39. cin >> n;
  40. cout << "Enter the elements of the matrix:\n";
  41. for (i = 0; i < n; i++)
  42. for (j = 0; j < n; j++)
  43. cin >> matrix[i][j];
  44. cout<<"The entered matrix is:"<<endl;
  45. for (i = 0; i < n; i++) {
  46. for (j = 0; j < n; j++)
  47. cout << matrix[i][j] <<" ";
  48. cout<<endl;
  49. }
  50. cout<<"Determinant of the matrix is "<< determinant(matrix, n);
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement