Giving only unequal results C++

Solving cubic equations. Needs to be improved by giving only unequal results (e.g. 0_0_3 -> 0_3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>
using namespace std;
 
int main()
{
    int coeff3, coeff2, coeff1, coeff0;
    int res;
    int i;
    int res1, res2, res3, res4;
    int discriminant;
    int root2, root3;
    int sqrtDiscriminant;
 
    cin >> coeff3 >> coeff2 >> coeff1 >> coeff0;
 
        for(i = 0; i < 10; i++)
        {
            res = (pow(i, 3) * coeff3) + (pow(i, 2) * coeff2) + (i * coeff1) + (coeff0);
            if(res == 0)
            {
                break;
            }
            else continue;
        }
        for(i = -10; i < 0; i++)
        {
            res = (pow(i, 3) * coeff3) + (pow(i, 2) * coeff2) + (i * coeff1) + (coeff0);
            if(res == 0)
            {
                break;
            }
            else continue;
        }
    res1 = (i * 0) + coeff3;
    res2 = (res1 * i) + coeff2;
    res3 = (res2 * i) + coeff1;
    res4 = (res3 * i) + coeff0;
 
    discriminant = ((res2 * res2) - (4 * res1 * res3));
    sqrtDiscriminant = sqrt(discriminant);
    root2 = ((-1 * res2) - (sqrtDiscriminant)) / (2 * res1);
    root3 = ((-1 * res2) + (sqrtDiscriminant)) / (2 * res1);
    cout <<i<<"_"<<root2<<"_"<<root3;
    return 0;
}
DellXT wrote:
Needs to be improved by giving only unequal results

Needs to be improved by actually solving cubics in the first place.

Try inputting 1 -1 1 -1

http://mathworld.wolfram.com/CubicFormula.html
Last edited on
Is it because of root from negative number?
Anyway, how to fix it?
DellXT wrote:
Anyway, how to fix it?


Your program needs a complete rewrite. It isn't going anywhere. Let me refer you again to that link:
http://mathworld.wolfram.com/CubicFormula.html

The fact that your coefficients are integers doesn't mean that the roots are. (Up to two of them may actually be complex, as in the example that I gave.)

...

Unless, of course, your original question (which you aren't telling us) actually says more; e.g. that the particular cubics being considered DO have equal real roots, in which case at least one of them would also be a root of the quadratic formed from differentiating the cubic.
Last edited on
But that's given in the problem - the input numbers are integers [-3000;3000];
output numbers also integers [-1000;1000].
What does the problem ACTUALLY SAY? In its ENTIRETY.

If your cubic equation is
x3 - x2 + x - 1 = 0
then the roots are 1, i and -i, where i is the square root of -1.
Last edited on
There's a cubic equation Ax^3+Bx^2+Cx+D=0
Find the roots that are integers [-1000;1000].
Input: integers A,B,C,D [-3000;3000]
Output: sort the roots in ascending way, separate them with symbol "_". Roots that are equal have to be printed only once.
In that case, it's probably quicker just to do it by trial:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
   const int N = 3;
   int coeff[N+1];
   const int xmin = -1000, xmax = 1000;
    
   cout << "Input coefficients, starting with highest power of X: ";
   for ( int p = N; p >= 0; p-- ) cin >> coeff[p];
 
   int numRoots = 0;
   for( int x = xmin; x <= xmax; x++)
   {
      int result = coeff[N];
      for ( int p = N - 1; p >= 0; p-- ) result = result * x + coeff[p];
      if ( result == 0 )
      {
         if ( numRoots > 0 ) cout << "_";
         numRoots++;
         cout << x;
         if ( numRoots == N ) break;
      }
   }
}


Input coefficients, starting with highest power of X: 1 -1 1 -1
1

Input coefficients, starting with highest power of X: 1 -1 -1 1
-1_1

Input coefficients, starting with highest power of X: 1 -6 11 -6
1_2_3
You saved my life! Thanks!
Are you past calc 1? If so, you can take the dx of this function easily (3*A*x*x + 2*B*x = C) and maybe code up a simple newtons method off that?

Brute force works too, but what do you do when that approach is not useful?
You don't have to do anything more here, but I am tossing some ideas out so you have some thoughts for next time.
Topic archived. No new replies allowed.