I wanted to write a program that substitutes the x and y values ​​for the equation:

5 / (x + y) = 2 / (x -y)

Here's what happened:

 #include <iostream> using namespace std; int main () { int x,y; int firstResult = 5 / (x + y); int secondResult = 2 / (x - y); for(x = 0; x < 200; x++){ for(y = 0; y < 200; y++){ if(firstResult == secondResult){ cout << "x= " << x << " y= " << y << endl; break; } else { cout << "fail" << endl; } } } return 0; } 

What is wrong here?

Closed due to the fact that off-topic participants are Vladimir Martyanov , Harry , iluxa1810 , Nicolas Chabanovsky 8 Nov '16 at 5:38 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • logical error. 0 can not be divided. cycle should start with 1 - Senior Pomidor
  • y = 3x / 7: x = 3 -> y = 9/7 - Igor
  • formula out of cycle, why ???? - nick_n_a
  • if "it is known that x = 3", why for(x = 0; x < 200; x++){ ? - Igor
  • just learning to solve problems using the exhaustive brute force - Ugnius Mkrthcan

3 answers 3

Even so ...

 #include <iostream> using namespace std; int main () { int x,y; for(x = 0; x < 200; x++){ for(y = 0; y < 200; y++){ if (x == y) continue; double firstResult = 5.0 / (x + y); double secondResult = 2.0 / (x - y); if (abs(firstResult - secondResult) < 1e-10) { cout << "x= " << x << " y= " << y << endl; return; } } } cout << "fail" << endl; return 0; } 

Although - it is clear that with positive x and y it makes no sense to check x , smaller y .

PS I hope you didn’t solve the equation, but practiced programming? :)

  • I'm just right now in the bookcase I'm learning I have made a mate. model of the real situation of the problem and decided to check the solution on the computer - Ugnius Mkrthcan
  • I just wanted to write about double - Senior Pomidor
  • and you have a more accurate result! - Senior Pomidor
  • Then 5(xy) = 2(x+y) , or 3x = 7y , or, if x==3 , then y = 9/7 , i.e. not a whole number ... - Harry
  • How to write a program for solving a system of equations for a more accurate result? - Ugnius Mkrthcan

There are several problems in your code:

  1. Using non-initialized variables;
  2. Integer division (+ division by 0);
  3. Calculating values ​​outside the loop body;
  4. Comparison of calculation results;
  5. Displays a failure message at each iteration.

All these problems can be solved :

 #include <iostream> #include <cmath> using namespace std; int main () { const double eps = 0.00001; bool found = false; for(int x = 0; x < 200; x++) { for(int y = 0; y < 200; y++) { double firstResult = 5. / (x + y); double secondResult = 2. / (x - y); if(std::abs(firstResult - secondResult) < eps) { cout << "x= " << x << " y= " << y << endl; found = true; break; } } } if(!found) { cout << "fail" << endl; } } 

    This equation does not have a sufficiently accurate solution. And the program below demonstrates this. That is, all the results of the calculation of the equation have large errors.

     #include <iostream> #include <limits> #include <cmath> int main() { const int N = 200; auto first_exp = []( int x, int y ) { return 5.0 / ( x + y ); }; auto second_exp = []( int x, int y ) { return 2.0 / ( x - y ); }; bool found = false; int x = 0, y = 0; for ( ; !found && x < N; x++ ) { for ( ; !found && y < N; y++ ) { if ( x != y ) { found = std::abs( first_exp( x, y ) - second_exp( x, y ) ) < std::numeric_limits<double>::epsilon(); } } } if ( found ) { std::cout << "x = " << x << ", y = " << y << std::endl; } else { std::cout << "There is no solution in the range 0 - " << N << std::endl; } return 0; } There is no solution in the range 0 - 200 

    Therefore, it is better to write the program so that it does not find the exact solution, but the best of all x and y values ​​on the interval [0, 200] . In this case, the program may look like this.

     #include <iostream> #include <cmath> int main() { const int N = 200; auto first_exp = []( int x, int y ) { return 5.0 / ( x + y ); }; auto second_exp = []( int x, int y ) { return 2.0 / ( x - y ); }; auto value = [&]( int x, int y ) { return std::abs( first_exp( x, y ) - second_exp( x, y ) ); }; bool first = true; int best_x = 0, best_y = 0; for ( int x = 0; x < N; x++ ) { for ( int y = 0; y < N; y++ ) { if ( x != y ) { if ( first || value( x, y ) < value( best_x, best_y ) ) { best_x = x; best_y = y; first = false; } } } } std::cout << "The best values are x = " << best_x << ", y = " << best_y << std::endl; return 0; } 

    The output of the program to the console:

     The best values are x = 7, y = 3