fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <cmath>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. // Function to calculate the determinant of a 2x2 matrix
  10. double determinant_2x2(const vector<vector<double>>& M) {
  11. return M[0][0] * M[1][1] - M[0][1] * M[1][0];
  12. }
  13.  
  14. // Function to solve the system using Cramer's Rule
  15. void cramers_rule(const vector<vector<double>>& A, const vector<double>& B) {
  16. int n = 2; // Fixed for 2x2 system
  17.  
  18. cout << setprecision(6) << fixed;
  19.  
  20. cout << "--- Cramer's Rule for 2x2 System ---" << endl;
  21. cout << "Coefficients Matrix A:" << endl;
  22. cout << setw(8) << A[0][0] << setw(8) << A[0][1] << endl;
  23. cout << setw(8) << A[1][0] << setw(8) << A[1][1] << endl;
  24. cout << "Constant Vector B:" << endl;
  25. cout << setw(8) << B[0] << endl;
  26. cout << setw(8) << B[1] << endl;
  27. cout << string(40, '-') << endl;
  28.  
  29.  
  30. double detA = determinant_2x2(A);
  31.  
  32. cout << "Output:" << endl;
  33. cout << "Determinant of Matrix A: " << detA << endl;
  34.  
  35. if (abs(detA) < 1e-9) {
  36. cout << "Matrix is singular; the system is not uniquely solvable." << endl;
  37. return;
  38. }
  39.  
  40. // Calculate Inverse of A (Optional step in problem, but included for completeness)
  41. double inv_detA = 1.0 / detA;
  42. vector<vector<double>> adjA = {
  43. {A[1][1], -A[0][1]},
  44. {-A[1][0], A[0][0]}
  45. };
  46.  
  47. cout << "Inverse of Matrix A:" << endl;
  48. cout << setw(8) << adjA[0][0] * inv_detA << setw(8) << adjA[0][1] * inv_detA << endl;
  49. cout << setw(8) << adjA[1][0] * inv_detA << setw(8) << adjA[1][1] * inv_detA << endl;
  50.  
  51.  
  52. // 1. Calculate Determinant of Ax1
  53. vector<vector<double>> Ax1 = A;
  54. Ax1[0][0] = B[0];
  55. Ax1[1][0] = B[1];
  56. double detAx1 = determinant_2x2(Ax1);
  57.  
  58. // 2. Calculate Determinant of Ax2
  59. vector<vector<double>> Ax2 = A;
  60. Ax2[0][1] = B[0];
  61. Ax2[1][1] = B[1];
  62. double detAx2 = determinant_2x2(Ax2);
  63.  
  64. // Calculate Solutions
  65. double x1 = detAx1 / detA;
  66. double x2 = detAx2 / detA;
  67.  
  68. cout << "The solution using Cramer's Rule:" << endl;
  69. cout << "x1 = " << x1 << endl;
  70. cout << "x2 = " << x2 << endl;
  71. cout << string(40, '-') << endl;
  72. }
  73.  
  74. int main() {
  75. // Sample Input based on the problem statement
  76. vector<vector<double>> A = {
  77. {2.0, 5.0},
  78. {-3.0, 1.0}
  79. };
  80. vector<double> B = {11.0, -4.0};
  81.  
  82. cramers_rule(A, B);
  83.  
  84. return 0;
  85. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
--- Cramer's Rule for 2x2 System ---
Coefficients Matrix A:
2.0000005.000000
-3.0000001.000000
Constant Vector B:
11.000000
-4.000000
----------------------------------------
Output:
Determinant of Matrix A: 17.000000
Inverse of Matrix A:
0.058824-0.294118
0.1764710.117647
The solution using Cramer's Rule:
x1 = 1.823529
x2 = 1.470588
----------------------------------------