### Trinomial factoring

I hate doing trinomials in class. The only way they taught us how to do them was guess and check.

(A trinomial looks like this: ax^2 + bx + c)
I made a C++ app that factors them into (x + )(x - ) form, and it also gives you the answers to X.

It only works with trinomials that look like this:
ax^2 + bx +c

Or ones that look like:
ax^2 + c

This kind doesn't work at this time:
ax^3 + bx + c

I would really appreciate it if someone could compile it and test a few trinomials in it. If there is a bug just say so. I might be able to fix it myself.

Trinomial factoring source code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` ``````#include #include #include #include using namespace std; double a, b, c, A, B, C, A2, ans1, ans2, ansgcd, q, t, x1, x2, answer; char title[] = "Factor Trinomials"; char equation[] = "ax^2 + bx + c"; char Exit = 'y'; void centerstring(char* string) { int l = strlen(string); int pos = (int)((80 - l) / 2); for (int i = 0; i < pos; i++) cout << " "; cout << string; } int gcd( int a, int b ) { c = 0; while(1) { a = a % b; if( a == 0 ) return b; b = b % a; if( b == 0 ) return a; } } double square_root(double t) { q = t / 2; while (1) { answer = 0.5*(q + (t / q)); if (q == answer) return answer; q = answer; } } int main() { while (Exit == 'y' || Exit == 'Y') { centerstring(title); cout << "\n"; centerstring(equation); cout << "\nA = "; cin >> A; cout << "B = "; cin >> B; cout << "C = "; cin >> C; ans1 = B - square_root( (B * B) - (4 * A * C)); ans2 = B + square_root( (B * B) - (4 * A * C)); A = A * 2; A2 = A; ansgcd = gcd(A, ans1); if (ansgcd < 0) { A = A / -ansgcd; ans1 = ans1 / -ansgcd; } else { A = A / ansgcd; ans1 = ans1 / ansgcd; } ansgcd = gcd(A2, ans2); if (ansgcd < 0) { A2 = A2 / -ansgcd; ans2 = ans2 / -ansgcd; } else { A2 = A2 / ansgcd; ans2 = ans2 / ansgcd; } cout << "\nAnswer is:\n("; if (A == 1.0) cout << "x"; else cout << A << "x"; if (ans1 < 0) cout << " - " << -ans1; else cout << " + " << ans1; cout << ")("; if (A2 == 1.0) cout << "x"; else cout << A2 << "x"; if (ans2 < 0) cout << " - " << -ans2; else cout << " + " << ans2; cout << ")"; x1 = -ans1 / A; x2 = -ans2 / A2; cout << "\nX = " << x1 << " or " << x2; do { cout << "\n\nFactor another? [y/n] "; cin >> Exit; } while (!Exit == 'n' && !Exit == 'N' && !Exit == 'y' && !Exit == 'Y'); cout << "\x1b[2J\x1b[H" << flush; } return 0; } ``````

Thanks.
Last edited on
cmath conveniently has a its own square root function, which may be more accurate than yours. Try it out. It's called sqrt().

Near the end of your program, you write:
 `` `` ``cout << "\x1b[2J\x1b[H" << flush;``

What is the purpose of this line?

Other than that, this seems quite functional. Well done. :-)

EDIT: Found a bug- when a,b, and c equal zero, it hangs. I suspect the problem is with gcd().
Last edited on
I made my own square root because sqrt was giving me problems. Something about it must be a double.

 `` `` ``cout << "\x1b[2J\x1b[H" << flush;``

That just clears the screen.

If your factoring a trinomial A,B and C will not equal 0.
Last edited on
Topic archived. No new replies allowed.