fork download
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4.  
  5. const int MAX = 10;
  6.  
  7. // Function to calculate determinant of a matrix
  8. double determinant(double mat[MAX][MAX], int n) {
  9. double det = 0;
  10. if(n == 1)
  11. return mat[0][0];
  12. if(n == 2)
  13. return (mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]);
  14.  
  15. double temp[MAX][MAX];
  16. int sign = 1;
  17.  
  18. for(int f=0; f<n; f++) {
  19. int subi = 0;
  20. for(int i=1; i<n; i++) {
  21. int subj = 0;
  22. for(int j=0; j<n; j++) {
  23. if(j == f)
  24. continue;
  25. temp[subi][subj] = mat[i][j];
  26. subj++;
  27. }
  28. subi++;
  29. }
  30. det += sign * mat[0][f] * determinant(temp, n-1);
  31. sign = -sign;
  32. }
  33. return det;
  34. }
  35.  
  36. // Function to get cofactor matrix for adjoint
  37. void getCofactor(double mat[MAX][MAX], double temp[MAX][MAX], int p, int q, int n) {
  38. int i = 0, j = 0;
  39. for(int row=0; row<n; row++) {
  40. for(int col=0; col<n; col++) {
  41. if(row != p && col != q) {
  42. temp[i][j++] = mat[row][col];
  43. if(j == n-1) {
  44. j = 0;
  45. i++;
  46. }
  47. }
  48. }
  49. }
  50. }
  51.  
  52. // Function to calculate adjoint
  53. void adjoint(double mat[MAX][MAX], double adj[MAX][MAX], int n) {
  54. if(n == 1) {
  55. adj[0][0] = 1;
  56. return;
  57. }
  58.  
  59. double temp[MAX][MAX];
  60. int sign = 1;
  61.  
  62. for(int i=0; i<n; i++) {
  63. for(int j=0; j<n; j++) {
  64. getCofactor(mat, temp, i, j, n);
  65. sign = ((i+j)%2 == 0) ? 1 : -1;
  66. adj[j][i] = sign * determinant(temp, n-1);
  67. }
  68. }
  69. }
  70.  
  71. // Function to calculate inverse
  72. void inverse(double mat[MAX][MAX], double inv[MAX][MAX], int n, double det) {
  73. double adj[MAX][MAX];
  74. adjoint(mat, adj, n);
  75.  
  76. for(int i=0; i<n; i++)
  77. for(int j=0; j<n; j++)
  78. inv[i][j] = adj[i][j] / det;
  79. }
  80.  
  81. int main() {
  82. int n;
  83. double A[MAX][MAX], B[MAX], X[MAX], Atemp[MAX][MAX], detA;
  84.  
  85. cout << "Enter the order of square matrix: ";
  86. cin >> n;
  87.  
  88. cout << "Enter coefficients of matrix A:\n";
  89. for(int i=0; i<n; i++)
  90. for(int j=0; j<n; j++)
  91. cin >> A[i][j];
  92.  
  93. cout << "Enter constants of vector B:\n";
  94. for(int i=0; i<n; i++)
  95. cin >> B[i];
  96.  
  97. // Find determinant of A
  98. detA = determinant(A, n);
  99. cout << fixed << setprecision(4);
  100. cout << "\nDeterminant of A: " << detA << endl;
  101.  
  102. if(detA == 0) {
  103. cout << "Matrix is singular; the system is not uniquely solvable.\n";
  104. return 0;
  105. }
  106.  
  107. // Solve using Cramer's Rule
  108. for(int i=0; i<n; i++) {
  109. // Copy A into Atemp
  110. for(int r=0; r<n; r++)
  111. for(int c=0; c<n; c++)
  112. Atemp[r][c] = A[r][c];
  113.  
  114. // Replace i-th column with vector B
  115. for(int r=0; r<n; r++)
  116. Atemp[r][i] = B[r];
  117.  
  118. double detAi = determinant(Atemp, n);
  119. X[i] = detAi / detA;
  120. }
  121.  
  122. // Display solution
  123. cout << "\nThe solution using Cramer's Rule:\n";
  124. for(int i=0; i<n; i++)
  125. cout << "x" << i+1 << " = " << X[i] << endl;
  126.  
  127. // Compute and display inverse (optional)
  128. double inv[MAX][MAX];
  129. inverse(A, inv, n, detA);
  130.  
  131. cout << "\nInverse of matrix A:\n";
  132. for(int i=0; i<n; i++) {
  133. for(int j=0; j<n; j++)
  134. cout << setw(8) << inv[i][j] << " ";
  135. cout << endl;
  136. }
  137.  
  138. return 0;
  139. }
  140.  
Success #stdin #stdout 0s 5320KB
stdin
2
2 5
-3 1
11 -4
stdout
Enter the order of square matrix: Enter coefficients of matrix A:
Enter constants of vector B:

Determinant of A: 17.0000

The solution using Cramer's Rule:
x1 = 1.8235
x2 = 1.4706

Inverse of matrix A:
  0.0588  -0.2941 
  0.1765   0.1176