Solve the system of two equations with two unknowns below:
a1, b1, c1, a2, b2 and c2 are inputted by the user himself.
I've been trying to find a math solution for the problem first and I can't seem to go far..
What I've tried so far is :
- From first equation to find y. (b1y = c1-a1x, y = (c1-a1x)/b1)
- Then I replace y in the second equation and I get one equation with 1 unknown in this case x. However, I can't solve the equation, I get some odd numbers / equations and stopped here.
Is this correct or is there an easier way to do this?
Current code:
#include <iostream>
using namespace std;
int main()
{
int a1, b1, c1, a2, b2, c2;
cout << "Enter the values for the first equation." << endl;
cout << "Enter the value for a1" << endl;
cin >> a1;
cout << "Enter the value for b1" << endl;
cin >> b1;
cout << "Enter the value for c1" << endl;
cin >> c1;
cout << "Enter the values for the second equation." << endl;
cout << "Enter the value for a2" << endl;
cin >> a2;
cout << "Enter the value for b2" << endl;
cin >> b2;
cout << "Enter the value for c2" << endl;
cin >> c2;
cout << "Your system of equations is the following:" << endl;
cout << a1 << "x+" << b1 << "y=" << c1 << endl;
cout << a2 << "x+" << b2 << "y=" << c2 << endl;
if ((a1 * b2) - (b1 * a2) == 0){
cout << "The system has no solution." << endl;
}
else{
res_x = ((c1*b2) - (b1*c2))/((a1*b2)-(b1*a2));
res_y = ((a1*c2) - (c1*a2)) / ((a1*b2) - (b1*a2));
cout << "x=" << res_x << " y=" << res_y << endl;
}
return 0;
}