Some math help.

I am making a app. that factors trinomials and am having some problems with it.

Trinomials are in the form of: ax^2 + bx + c
The formula for factoring those is: 2ax + b ± sqrt(b2 - 4ac)
Page were I found that: http://209.85.165.104/search?q=cache:6cpFf3EVZH4J:www.miracosta.cc.ca.us/home/pmcguire/Trinomials.doc+trinomial+formula&hl=en&ct=clnk&cd=10&gl=us

My source code:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;

int egsit = false;
int a, b, c, A, B, C, A2, ans1, ans2;
char title[] = "Factor Trinomials";
char formula[] = "ax^2 + bx + c";	

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)
{
    while(1)
    {
  		c = a % b;
  		if (c == 0)
  			return b;
  		a = b;
  		b = c;
    }
}

int main()
{
	while (egsit == false)
	{
		centerstring(title);
		cout << "\n";
		centerstring(formula);
		cout << "\nA = ";
		cin >> A;
		cout << "B = ";
		cin >> B;
		cout << "C = ";
		cin >> C;
		
		ans1 = B + (sqrt( (B)^2 - (4 *A * C)));
		ans2 = B - (sqrt( (B)^2 - (4 *A * C)));
		a *= 2;
		gcd(A, ans1);
		A /= b;
		ans1 /= b;
		gcd(A, ans2);
		A2 /= b;
		ans2 /= b;
		
		cout << "Answer is:\n(" << A << "x";
		if (ans1 < 0)
		{
			ans1 /= -1;
			cout << " - ";
		}http://www.google.com/
		else
			cout << " + ";
		
		cout << ans1 << ")(" << A2 << "x";
		if (ans2 < 0)
		{
			ans2 /= -1;
			cout << " - ";
		}
		else
			cout << " + ";
		
		cout << ans2 << ")";
		
		do 
		{
			cout << "\n\nFactor another? [y/n] ";
			cin >> egsit;
			if (egsit == 'y' | egsit == 'Y')
				egsit = false;
			else if (egsit == 'n' | egsit == 'N')
				egsit = true;
			else 
				egsit = 2;
		} while (egsit == 2);
		cout << "\x1b[2J\x1b[H" << flush;
	}
	return 0;
}


When I compile it outputs:
1
2
3
temp.cpp:48: warning: converting to ‘int’ from ‘double’
temp.cpp:49: warning: converting to ‘int’ from ‘double’
Compilation finished successfully.

Which is weird because ans1 and ans2 are declared as int's. And If I use the example trinomial on the website, ans1 should equal 20 and ans2 should equal -24.

When I run the example trinomial on the website, I input A, B and C exactly as said and it outputs:
 
Floating point exception (core dumped)


Any idea on how to fix this?
Last edited on
Heres some hints:

-- The logical OR operator is ||. | is the bitwise OR operator.
-- ^ is the bitwise XOR operator.
-- sqrt functions accepts float point numbers not ints. Thats why you are getting warnings.

The operations that you might be looking for is here:
http://www.cplusplus.com/reference/clibrary/cmath/

Jeff
Thanks I'll check that out.
Topic archived. No new replies allowed.