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

ShawnCplus 0 Tallied Votes 3K Views Share

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

#include <iostream>
#include <windows.h>
using namespace std;

int main(){
    int Deter, DeterX, DeterY, A1, A2, B1, B2, C1, C2;
    int B1Sign, B2Sign, DeterX_Dem, DeterY_Dem;
    bool Det_Ind=false;
    
    SetConsoleTitle("Cramer's Rule");
    cout<<"Program Written By Shawncplus To Solve a Linear Equation with Cramer's Rule"<< endl;
    
    cout<<"Please Enter a1: ";
    cin>>A1;
    cout<<"\nPlease Enter b1: ";
    cin>>B1;
    cout<<"\nPlease Enter c1: ";
    cin>>C1;
    cout<<"\nPlease enter a2: ";
    cin>>A2;
    cout<<"\nPlease enter b2: ";
    cin>>B2;
    cout<<"\nPlease Enter c2: ";
    cin>>C2;
    cout<<"The Equation is {\n";
    B1Sign=B1*(-1);
    B2Sign=B2*(-1);
    if (B1>0){
       cout<<"\t "<< A1 <<"x+"<<B1<<"y = "<< C1<< endl;}
    else if (B1<0){
       cout<<"\t "<< A1 <<"x-"<<B1Sign<<"y = "<< C1<< endl;}
    if (B2>0){
       cout<<"\t "<< A2 <<"x+"<<B2<<"y = "<< C2<< "\n\t\t} " <<endl;}
    else if (B2<0){
       cout<<"\t "<< A2 <<"x-"<<B2Sign<<"y = "<< C2<< "\n\t\t} " << endl;}
     
    Deter=(A1*B2)-(A2*B1);
    DeterX=(C1*B2)-(C2*B1);
    DeterY=(A1*C2)-(A2*C1);
    if (Deter==0 && DeterX==0 && DeterY==0) {
                 cout<<"System is Dependant (Infinite Solutions)"<< endl;
                 Det_Ind = true;
                 }
    else if (Deter==0 && (DeterX!=0 || DeterY!=0)){
                 cout<<"System is Inconsistent (System Set is \xE9 )"<< endl;
                 Det_Ind = true;
                 }
    
    cout<<"D    = "<< Deter << endl; 
    cout<<"D(x) = "<< DeterX << endl;
    cout<<"D(y) = "<< DeterY << endl;

    if (Det_Ind==true){
                   cout<<"Please press Enter to continue. . .";
                   cin.get();
                   cin.get();
                   return 0;
                   }

    else if (Det_Ind==false){               
            DeterX_Dem=(DeterX/Deter);
            DeterY_Dem=(DeterY/Deter);
            cout<<"\nX = "<< DeterX<<"/"<< Deter <<" = "<< DeterX_Dem << endl;
            cout<<"Y = "<< DeterY<<"/"<< Deter <<" = "<< DeterY_Dem << endl;
            cout<<"Solution Set is {("<< DeterX_Dem <<","<< DeterY_Dem <<")}"<< endl;
            }           
  
    cout<<"\n\nPlease press Enter to continue. . .";
    cin.get();
    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.