ax3+bx2+cx+d=0 - do.. while | Anyone, help?

Hi there, let me explain what i need to accomplish.
So my task is to create this ax3+bx2+cx+d=0 mathematical solution with do.. while operator.

Task described:

Integers are entered a, b, c, d. Find all ax3+bx2+cx+d=0 roots of the equation.
Root's have to be found in "d" divisor. For example d=5 - which divides with 5 and 1 and the same with negatives, if i understood correctly. And then those divisors have to be placed in x'es of equitation. If current equitation result = 0 and if it is 0 then divisor is the root.

Hope i made a point.

So this is mine code, i would be really grateful if You could review these several lines of code, and comment if everything is in correct place and working as it should be, and if you are familiar with this you could throw a little advise how to improve it if there is something to add.


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
47
48
49
50
51
52
53
54
  #include <iostream>

using namespace std;

int main()
{
	int a, b, c, d, x1, x2;

	cout << "Enter a: ";
	cin >> a;
	cout << "Enter b: ";
	cin >> b;
	cout << "Enter c: ";
	cin >> c;
	cout << "Enter d: ";
	cin >> d;
	cout << "Enter divisor of d :";
	cin >> x1;
	cout << "Enter divisor of d :";
	cin >> x2;
	
	int i = 1;

	do
	{
		if (d % i == 0)

		{
			if (a, b, c, d, i == 0)
				cout << "Root = 0" << i << endl;

			else cout << "root =" << i;
			cout << endl;

			if (a, b, c, d, -i == 0)
				cout << " - Root = 0 " << -i << endl;

			else cout << " - root =" << -i;
			cout << endl;
			
			
		}

		if (a * x1 * x2 * x1 * x2 * x1 * x2 + b * x1 * x2 * x1 * x2 + c * x1 * x2 + d == 0)
			cout << "Root of D = 0" << endl;

		else cout << "Root of D is not 0" << endl;

		i++;
	} while (i <= d / 2);
	
	return 0;
}


I would appreciate if someone could review this today, so i can rest in peace. i have a stupid deadline for this till tomorrow. Very big thanks.
Last edited on
What were you trying to say on lines 29 and 35?
This does not look like the values of x are changing either. I doubt this is a solution.
Perhaps some reading about root finding of polynomials will help:
http://en.wikipedia.org/wiki/Root-finding_algorithm#Finding_roots_of_polynomials
http://en.wikipedia.org/wiki/Bairstow%27s_method
Last edited on
I have removed line 29 and 35, haven't thought it fully out.
i did read, but that did even mess my head even more.

x values i try to enter manually, for example if d = 2, in x1 I enter 1 and in x2 I enter 2, the only divisors which can divide number 2.

im thinking it to do manually, cause i have no idea how to put in x'es all possible divisors automatically
Finding all the divisors of a number is trivial. A for loop can be used to test all natural numbers less than or equal to the number in question. This trivial solution is definitely not the most efficient, but it will do the job well enough for small numbers.
I posted an example here:
http://coliru.stacked-crooked.com/a/d8db75658c3fae12
Very good, thank you, ill let you know the outcome as soon i will have implemented it in code.

Thanks.
Topic archived. No new replies allowed.