I have an assignment on zybooks for my c coding course and whenever I run my code it outputs every outcome no matter if it's right or wrong. My Code:
#include <iostream>
using namespace std;
int main() {
int x1;
int y1;
int ans1;
int x2;
int y2;
int ans2;
int x;
int y;
cin >> x1 >> y1 >> ans1;
cin >> x2 >> y2 >> ans2;
for (x = -10; x <= 10; x ) {
for (y = -10; y <= 10; y ) {
if ((ans1 = (x1 * x) (y1 * y)) = (ans2 = (x2*x) (y2*y))) {
cout << "x = " << x << ", " << "y = " << y << endl;
}
}
}
return 0;
}
The instructions are: Numerous engineering and scientific applications require finding solutions to a set of equations. Ex: 8x 7y = 38 and 3x - 5y = -1 have a solution x = 3, y = 2. Given integer coefficients of two linear equations with variables x and y, use brute force to find an integer solution for x and y in the range -10 to 10.
Ex: If the input is:
8 7 38
3 -5 -1
the output is:
x = 3, y = 2
Use this brute force approach:
For every value of x from -10 to 10
For every value of y from -10 to 10
Check if the current x and y satisfy both equations. If so, output the solution, and finish
Ex: If no solution is found, output:
There is no solution
Assume the two input equations have no more than one solution.
Then when I run the program my output is:
x = -10, y = -10
x = -10, y = -9
x = -10, y = -8
...
going all the way to x = 10 and y = 10
CodePudding user response:
All the =
signs in (ans1 = (x1 * x) (y1 * y)) = (ans2 = (x2*x) (y2*y))
are assignments, not comparisons.
Clang even warns you about this:
.code.tio.cpp:19:42: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
if ((ans1 = (x1 * x) (y1 * y)) = (ans2 = (x2*x) (y2*y))) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
.code.tio.cpp:19:42: note: place parentheses around the assignment to silence this warning
if ((ans1 = (x1 * x) (y1 * y)) = (ans2 = (x2*x) (y2*y))) {
^
( )
.code.tio.cpp:19:42: note: use '==' to turn this assignment into an equality comparison
if ((ans1 = (x1 * x) (y1 * y)) = (ans2 = (x2*x) (y2*y))) {
^
==
1 warning generated.
You should thus use comparisons like this:
for (x = -10; x <= 10; x ) {
for (y = -10; y <= 10; y ) {
if ((x1 * x y1 * y == ans1) && (x2*x y2*y == ans2)) {
cout << "x = " << x << ", " << "y = " << y << endl;
}
}
}
CodePudding user response:
Here, I had solved it as simple as I can and commented some of the logics.
#include <iostream>
using namespace std;
int main() {
int x1;
int y1;
int ans1;
int x2;
int y2;
int ans2;
int x;
int y;
cin >> x1 >> y1 >> ans1;
cin >> x2 >> y2 >> ans2;
int flag = 0;
for (x = -10; x <= 10; x ) {
for (y = -10; y <= 10; y ) {
if (ans1 == (x1 * x) (y1 * y) && ans2 == (x2*x) (y2*y) ) { /*if we got the root of 1st equation then using bitwise and we have to compare that with another equation*/
cout << "x = " << x << ", " << "y = " << y << endl;
flag=1; /* If we found the ans then we will raise the flag to 1, So after the end of loop it'll not print the below statement*/
break;
}
}
}
if(flag==0){
cout<<"There is no solution\n";
}
return 0;
}