Solve a 2x2 linear equation matrix with Cramer's Rule

Please support our C++ advertiser: Intel Parallel Studio Home
ShawnCplus ShawnCplus is offline Offline Feb 6th, 2007, 7:24 pm |
0
This program was inspired by my college math 1 class. It takes the values of a1, a2, b1, b2, c1, c2 and solves, displays the equation and shows the work. It also stops if the equation is dependant or inconsistent (Zero division)
Cramer's Rule
if a1x+b1y=c1 and a2x+b2y=c2

then x=((c1*b2)-(c2*b1))/((a1*b2)-(a2*b1))
y=((a1*c2)-(a2*c1))/((a1*b2)-(a2*b1))

where (a1*b2)-(a2*b1)!=0

#Compiled on Dev-C++

#Comment or post bugs please
Quick reply to this message  
C++ Syntax
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. int main(){
  6. int Deter, DeterX, DeterY, A1, A2, B1, B2, C1, C2;
  7. int B1Sign, B2Sign, DeterX_Dem, DeterY_Dem;
  8. bool Det_Ind=false;
  9.  
  10. SetConsoleTitle("Cramer's Rule");
  11. cout<<"Program Written By Shawncplus To Solve a Linear Equation with Cramer's Rule"<< endl;
  12.  
  13. cout<<"Please Enter a1: ";
  14. cin>>A1;
  15. cout<<"\nPlease Enter b1: ";
  16. cin>>B1;
  17. cout<<"\nPlease Enter c1: ";
  18. cin>>C1;
  19. cout<<"\nPlease enter a2: ";
  20. cin>>A2;
  21. cout<<"\nPlease enter b2: ";
  22. cin>>B2;
  23. cout<<"\nPlease Enter c2: ";
  24. cin>>C2;
  25. cout<<"The Equation is {\n";
  26. B1Sign=B1*(-1);
  27. B2Sign=B2*(-1);
  28. if (B1>0){
  29. cout<<"\t "<< A1 <<"x+"<<B1<<"y = "<< C1<< endl;}
  30. else if (B1<0){
  31. cout<<"\t "<< A1 <<"x-"<<B1Sign<<"y = "<< C1<< endl;}
  32. if (B2>0){
  33. cout<<"\t "<< A2 <<"x+"<<B2<<"y = "<< C2<< "\n\t\t} " <<endl;}
  34. else if (B2<0){
  35. cout<<"\t "<< A2 <<"x-"<<B2Sign<<"y = "<< C2<< "\n\t\t} " << endl;}
  36.  
  37. Deter=(A1*B2)-(A2*B1);
  38. DeterX=(C1*B2)-(C2*B1);
  39. DeterY=(A1*C2)-(A2*C1);
  40. if (Deter==0 && DeterX==0 && DeterY==0) {
  41. cout<<"System is Dependant (Infinite Solutions)"<< endl;
  42. Det_Ind = true;
  43. }
  44. else if (Deter==0 && (DeterX!=0 || DeterY!=0)){
  45. cout<<"System is Inconsistent (System Set is \xE9 )"<< endl;
  46. Det_Ind = true;
  47. }
  48.  
  49. cout<<"D = "<< Deter << endl;
  50. cout<<"D(x) = "<< DeterX << endl;
  51. cout<<"D(y) = "<< DeterY << endl;
  52.  
  53. if (Det_Ind==true){
  54. cout<<"Please press Enter to continue. . .";
  55. cin.get();
  56. cin.get();
  57. return 0;
  58. }
  59.  
  60. else if (Det_Ind==false){
  61. DeterX_Dem=(DeterX/Deter);
  62. DeterY_Dem=(DeterY/Deter);
  63. cout<<"\nX = "<< DeterX<<"/"<< Deter <<" = "<< DeterX_Dem << endl;
  64. cout<<"Y = "<< DeterY<<"/"<< Deter <<" = "<< DeterY_Dem << endl;
  65. cout<<"Solution Set is {("<< DeterX_Dem <<","<< DeterY_Dem <<")}"<< endl;
  66. }
  67.  
  68. cout<<"\n\nPlease press Enter to continue. . .";
  69. cin.get();
  70. cin.get();
  71. return 0;
  72. }

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC