Quadratic formula factoriser

Hey, i'm trying to create a program that factorises quadratics but i'm having trouble with a certain aspect.

At the moment it factorises if a, b, and c are positive, and factorises if a and c are positive with b negative.

Now i'm trying to do the last one where a is positive, c is negative, and b is positive/negative depending on the numbers used but i'm struglling so i decided to add in some output statements to see if i'm getting the desired results but i either get nothing or garbage results.
EDIT** Sorry, to be more specific... I'm using arrays to hold the factors of C which i then use to add/subtract to get B but when i try and cycle through the array outputting each element i get nothing or garbage results

So i guess a good starting question would be, can arrays hold negative integers?
Last edited on
arrays can hold any type you want. int, unsigned, float, char, whatever.

are you not using the quadratic formula?
*scratches head*... umm... could you post some code?

What do you mean by quadratics? Something like ax^2+bx+c = a(x-(-b+sqrt(b*b-4*a*c))/(2*a) ) (x-(-b-sqrt(b*b-4*a*c))/(2*a) ) ( http://en.wikipedia.org/wiki/Quadratic_equation ) or do you mean something more complicated?
No i'm not, i'm using the method where you find what multiplies to make C and adds/subtracts to make B e.g. ( all positive )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
             for(i=0;i<counter;i++)
              {
                for(x=0;x<=counter;x++)
                  if((factors[i]*factors[x]==c) && (factors[i]+factors[x])==b)
                    {
                      cout << "\n\nFactorises to...\n\n";
                      cout << "  (x + " << factors[i] << ")(x + " << factors[x] << ")";
                      cout << "\n\n";
                      cout << "x = " << factors[i]*-1;
                      cout << "   or   x = " << factors[x]*-1;
                      cout << "\n\n";

                      return;
                    }
              }


where 'counter' is the amount of factors of C that are stored and 'factors[]' is clearly the actual factors.

- I wanted a bit more of a challenge than using the quadratic formula.
Last edited on
Very nice!

Some notes: this method requires that a be equal to 1!
The factorization will work only if your equation has rational solutions. Otherwise it will fail to factor your quadratic.

The formula is absolutely the same whether you use positive or negative numbers. If in factors you already precomputed the positive divisors of c, it will suffice simply to enlarge factors by adding the negative divisors of c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int* NegativeFactorsIncluded= new int[counter*2];
for (int i=0;i<counter;i++)
{ 
  NegativeFactorsIncluded[i]= factors[i];
  NegativeFactorsIncluded[i+counter]= -factors[i];
}

for(i=0;i<2*counter;i++)
{
  for(x=0;x<=2*counter;x++)
    if( (NegativeFactorsIncluded[i]*NegativeFactorsIncluded[x]==c) &&
        (NegativeFactorsIncluded[i]+NegativeFactorsIncluded[x])==b)
    { 
       cout << "\n\nFactorises to...\n\n";
       cout << "  (x + " << NegativeFactorsIncluded[i] << ")(x + " 
            << NegativeFactorsIncluded[x] << ")";
       cout << "\n\n";
       cout << "x = " << NegativeFactorsIncluded[i]*(-1);
       cout << "   or   x = " << NegativeFactorsIncluded[x]*(-1);
       cout << "\n\n";
       return;
     }
}
delete [] NegativeFactorsIncluded;

Last edited on
@ tition
... to them first two notes I know dont worry, i've actually got a switch case set up so that the quadratic will be hadnled differently in each case, and I only wanted rational solutions so that they could be putput as I have shown in my last post.

The problem is that when I send the values to the array I wanted to make sure that it was working properly but i was getting garbage results. For example, for -12 i was only getting 3 results, one of which was above 100,000 :S

Thank you for the reply.
Topic archived. No new replies allowed.