I need help figuring out what the problem is with this code for my homework. The question is- if ad-bc is 0, the equation has no solution and is Solvable should be false.
Write a program that prompts the user to enter a,b,c,d,e, and f and displays the result. If ad - bc is 0, report that "The equation has no solution."
My code is right here:
header file "HeaderA.h" :
1 2 3 4 5 6 7 8 9 10
#include <iostream>
usingnamespace std;
void solveEquation(double a, double b, double c, double d, double e, double f, double &x, double &y, bool &isSolvable)
{
e = (a * x) + (b * y);
f = (c * x) + (d * y);
x = ((e * d) - (b * f)) / ((a * d) - (b * c));
y = ((a * f) - (e * c)) / ((a * d) - (b * c));
}
Also the solution is (bool)(a * d) - (b * c); as you state in your code, or is something wrong? If so then why you use e,f,x,y if you don't use them?
To solve your question why you always get 1 as a result you have declared isSolvable as bool meaning 0 (false) or 1 (true). Every value different than 0 is considered true thus 1.
Declare isSolvable as double to keep its value and compare its value with 0 to get the correct message. I whould advice to use a syntax like:
if(abs((a * d) - (b * c) < 1e-8)) instead of a comparison with 0 as doubles can be tricky with such comparisons.
1e-8 is just an example you can use any suitable value.
You can also declare variables this way: double a,b,c,d,e,f,x,y;.