Factoring Program - Strange Error

So I wrote a polynomial factoring program just to see if I could do it. Everything went fairly well, the program acts as it is suppose to except when the c in Ax^2+Bx+c is right around -25 or less, it will work with -20 -15 -2 20 2 but not -30 or -60.

It's interesting to note that the dynamically created array on the second attempt shown does not have all the correct factors of 60 in it, it starts with 15 * 1 and then the rest are correct. So do I have something wrong in my factoring function or did I do something wrong with the dynamically allocated memory? It seems that something happens when c is < ~-25 and I delete the arrays at the end. If I do NOT make a call to deleteArray at the end of main the program will work fine every time, even for c = -60.

I am working on getting my debugger working over here so if this is really obvious please forgive me. I only started learned C++ a couple weeks ago, perhaps I shouldn't have went off on my own here to try this.

Thanks very very much in advance though for any help!

PS I am writing this on:

Gentoo Base System 2.1 (Hardened w/ PaX & w/o grsecurity or SELinux)
GCC 4.5.3-hardened with PIE and SSP
core2 duo 2.4ghz
2 gb ram

Code::Blocks as an IDE
gdb as debugger

EDIT 2: I totally screwed up the this post and I also noticed some dumb mistakes so I cleaned up my files and posted them in separate posts below the errors shown in post 2. Unfortunately my silly mistakes were not the cause of the error I am asking about as they still happen in the updated program.

Thanks again!
Last edited on
EDIT THE ERRORS I GET ARE BELOW HERE, MY POST WAS TOO LONG TO FIT IT ALL IN, SHOULD HAVE BEEN THE OTHER WAY, oh well.... (caps just to try to get attention down here)

I have it set to use 1 8 15 initially, it will ask if this is correct, if you say no then you can input your own coefficients. Normally I get an output like this:
|-(bobby@Desktop)-(0)-(12:18 AM Fri Dec 23)->
|-(~/Cpp Projects/FactoringTool/bin/Debug)-(1 files, 52Kb)--> ./FactoringTool
The polynomial you entered has coefficients:
A: 1
B: 8
C: 15
Does this look correct?
1. Yes
2. No
2
Please enter the coefficient for x^2: 1

Please enter the coefficient for x: -2

Please enter the constant term (c): -15

The polynomial you entered has coefficients:
A: 1
B: -2
C: -15

Factoring C (-15)......


Format of results:

Factor on top
Divisor on bottom

15 5 3 1
1 3 5 15

With a total of 4 possible factors to use.


Testing these factors.......

in (x+a)(x+b)
a is -5
and b is 3

So your final form is (x-5)(x+3)


but if you use too large of a negative number you get this:

|-(bobby@Desktop)-(0)-(12:20 AM Fri Dec 23)->
|-(~/Cpp Projects/FactoringTool/bin/Debug)-(1 files, 52Kb)--> ./FactoringTool
The polynomial you entered has coefficients:
A: 1
B: 8
C: 15
Does this look correct?
1. Yes
2. No
2
Please enter the coefficient for x^2: 1

Please enter the coefficient for x: -4

Please enter the constant term (c): -60

The polynomial you entered has coefficients:
A: 1
B: -4
C: -60

Factoring C (-60)......


Format of results:

Factor on top
Divisor on bottom

15 30 20 15 12 10 6 5 4
1 2 3 4 5 6 10 12 15

With a total of 9 possible factors to use.


Testing these factors.......

in (x+a)(x+b)
a is -10
and b is 6

So your final form is (x-10)(x+6)
*** glibc detected *** ./FactoringTool: free(): invalid next size (fast): 0x00000069a6ae2230 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x76115)[0x318c1b54115]
/lib64/libc.so.6(cfree+0x6c)[0x318c1b5904c]
./FactoringTool(+0x1504)[0x69a68d4504]
./FactoringTool(main+0x702)[0x69a68d4ce2]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x318c1b0020d]
./FactoringTool(+0xbb9)[0x69a68d3bb9]
======= Memory map: ========
69a68d3000-69a68d6000 r-xp 00000000 08:03 15467796 /home/bobby/Cpp Projects/FactoringTool/bin/Debug/FactoringTool
69a6ad5000-69a6ad6000 r--p 00002000 08:03 15467796 /home/bobby/Cpp Projects/FactoringTool/bin/Debug/FactoringTool
69a6ad6000-69a6ad7000 rw-p 00003000 08:03 15467796 /home/bobby/Cpp Projects/FactoringTool/bin/Debug/FactoringTool
69a6ad7000-69a6b04000 rw-p 00000000 00:00 0 [heap]
318bc000000-318bc021000 rw-p 00000000 00:00 0
318bc021000-318c0000000 ---p 00000000 00:00 0
318c1ade000-318c1c63000 r-xp 00000000 08:03 12871890 /lib64/libc-2.14.1.so
318c1c63000-318c1e62000 ---p 00185000 08:03 12871890 /lib64/libc-2.14.1.so
318c1e62000-318c1e66000 r--p 00184000 08:03 12871890 /lib64/libc-2.14.1.so
318c1e66000-318c1e67000 rw-p 00188000 08:03 12871890 /lib64/libc-2.14.1.so
318c1e67000-318c1e6c000 rw-p 00000000 00:00 0
318c1e6c000-318c1e81000 r-xp 00000000 08:03 8913988 /lib64/libgcc_s.so.1
318c1e81000-318c2080000 ---p 00015000 08:03 8913988 /lib64/libgcc_s.so.1
318c2080000-318c2081000 r--p 00014000 08:03 8913988 /lib64/libgcc_s.so.1
318c2081000-318c2082000 rw-p 00015000 08:03 8913988 /lib64/libgcc_s.so.1
318c2082000-318c2103000 r-xp 00000000 08:03 12865885 /lib64/libm-2.14.1.so
318c2103000-318c2302000 ---p 00081000 08:03 12865885 /lib64/libm-2.14.1.so
318c2302000-318c2303000 r--p 00080000 08:03 12865885 /lib64/libm-2.14.1.so
318c2303000-318c2304000 rw-p 00081000 08:03 12865885 /lib64/libm-2.14.1.so
318c2304000-318c2402000 r-xp 00000000 08:03 11966648 /usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/libstdc++.so.6.0.14
318c2402000-318c2602000 ---p 000fe000 08:03 11966648 /usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/libstdc++.so.6.0.14
318c2602000-318c260b000 r--p 000fe000 08:03 11966648 /usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/libstdc++.so.6.0.14
318c260b000-318c260c000 rw-p 00107000 08:03 11966648 /usr/lib64/gcc/x86_64-pc-linux-gnu/4.5.3/libstdc++.so.6.0.14
318c260c000-318c2620000 rw-p 00000000 00:00 0
318c2620000-318c2641000 r-xp 00000000 08:03 12872301 /lib64/ld-2.14.1.so
318c2808000-318c280d000 rw-p 00000000 00:00 0
318c283a000-318c283e000 rw-p 00000000 00:00 0
318c283e000-318c2840000 r-xp 00000000 00:00 0 [vdso]
318c2840000-318c2841000 r--p 00020000 08:03 12872301 /lib64/ld-2.14.1.so
318c2841000-318c2842000 rw-p 00021000 08:03 12872301 /lib64/ld-2.14.1.so
318c2842000-318c2843000 rw-p 00000000 00:00 0
3a0ffb7f000-3a0ffba1000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r--p 00000000 00:00 0 [vsyscall]
Aborted

Last edited on
Factor.cpp

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "Factor.h"
using namespace std;

Factor::Factor(int A, int B, int C)
{

    a = A;
    b = B;
    c = C;

}

// function to factor the third term in a polynomial
void Factor::cFactor(int C)
{
    // we just want the positive factors of C, leave out -1
    // this will be tested for by the testFactors function
    if (C < 0)
    {
        C=-C;
    }
    numberOfFactors = 0;

    pdivisor = new int[numberOfFactors];
    pfactors = new int[numberOfFactors];
    // calculation phase
    for(int iii=1;iii<20;iii++) // note that 20 is arbitrary, for testing purposes I limited the factors up to 20, should be enough for now
    {
        if ((C % iii) == 0)
        {
            pdivisor[numberOfFactors] = iii; // keep track of evenly divisable numbers
            pfactors[numberOfFactors] = (C / iii); // place the cooresponding factor in another array but the same column

            numberOfFactors++;
        }
    }

    //termination phase
    cout << "\nFormat of results: \n\n" << "Factor on top\n" << "Divisor on bottom\n" << endl;

    for (int nCol=0; nCol < numberOfFactors; nCol++)
    {
        cout << pfactors[nCol] << "\t";
    }
    cout << endl;
    for (int nCol=0; nCol < numberOfFactors; nCol++)
    {
        cout << pdivisor[nCol] << "\t";
    }

    cout << "\n\nWith a total of " << numberOfFactors << " possible factors to use." << endl;
} // end c factorization function

// function to get the current value of A
int Factor::getA()
{
    return a;
}

//function to get the current value of B
int Factor::getB()
{
    return b;
}

// function to get the current value of C
int Factor::getC()
{
    return c;
}

// function to get number of factors
int Factor::getNumberOfFactors()
{
    return numberOfFactors;
}

void Factor::testFactors()
{
    cout << "\n\nTesting these factors.......\n" << endl;

    for(int iii=0;iii<numberOfFactors;iii++)
    {
        // testing with positive factors
        if(pdivisor[iii]+pfactors[iii] == b)
        {
            if(pdivisor[iii]*pfactors[iii] == c)
            {
                factoreda = pdivisor[iii];
                factoredb = pfactors[iii];
            }
        }

        // testing with negative
        if (-pdivisor[iii]+pfactors[iii] == b)
        {
            if(-pdivisor[iii]*pfactors[iii] == c)
            {
                factoreda = -pdivisor[iii];
                factoredb = pfactors[iii];
            }
        }

        // testing with both factors negative
        if (-pdivisor[iii]-pfactors[iii] == b)
        {
            if(-pdivisor[iii]*(-pfactors[iii]) == c)
            {
                factoreda = -pdivisor[iii];
                factoredb = -pfactors[iii];
            }
        }

    }
}

int Factor::getFactoreda()
{
    return factoreda;
}

int Factor::getFactoredb()
{
    return factoredb;
}

void Factor::setA(int A)
{
    a = A;
}

void Factor::setB(int B)
{
    b = B;
}

void Factor::setC(int C)
{
    c = C;
}

void Factor::deleteArray()
{
    delete [] pdivisor;
    delete [] pfactors;
}
Last edited on
Factor.h

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
#ifndef FACTOR_H_INCLUDED
#define FACTOR_H_INCLUDED
#include <iostream>
#include <string>
#include <iomanip>


// class definition for factoring tool
class Factor
{
public:
    Factor(int A, int B, int C); // constructor
    void cFactor(int C); // c factorizor (not necessarily prime factors, but they are included)
    int getA(); // function to return the current value of A
    int getB(); // function to return the current value of B
    int getC(); // function to return the current value of C
    int getNumberOfFactors(); // returns the integer number of factors found by the cFactor function
    void testFactors(); // tests factors based on a number of conditions to cover all possibilities
    int getFactoreda(); // returns the proper factor to use with sign
    int getFactoredb(); // returns other proper factor to use with sign
    void setA(int); // allows user to set the coefficient A
    void setB(int); // allows user to set the coefficient B
    void setC(int); // allows user to set the coefficient C
    void deleteArray(); // deletion of dynamically allocated memory


private:
    int a; // coefficient of second order term
    int b; // coefficient of first order term
    int c; // constant offset term
    int numberOfFactors; // variable to hold number of factors
    int *pdivisor; // pointer that is used to create a dynamic array to store "good" divisors
    int *pfactors; // pointer that is used to create a dynamic array to store the results of devision (cooresponding factor)
    int factoreda; // variable to store the correct factor value, signed
    int factoredb; // variable to store the other correct factor value, signed
}; // end class definition


#endif // FACTOR_H_INCLUDED 
Last edited on
main.cpp

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 "Factor.h"

using namespace std;

int main()
{
    Factor myFactor(1, 8, 15); //initialization of constructor

    // tell user what the current setting initialized by the constructor are
    cout << "The polynomial you entered has coefficients:\n" << "A: " << myFactor.getA()
     << "\nB: " << myFactor.getB() << "\nC: " << myFactor.getC() << endl;

    // ask if they would like to change the default choice
     int choice;
     do
     {
         cout << "Does this look correct?\n"
            << "1. Yes\n2. No" << endl;
            cin >> choice;
     } while(choice != 1 && choice != 2);

    // for a quick demo choose 1
     if (choice == 1)
    {
        cout << "Factoring C (" << myFactor.getC() <<")......\n" << endl;

        myFactor.cFactor(myFactor.getC());
    }

    // to enter your own number choose 2
    if (choice == 2)
    {
        cout << "Please enter the coefficient for x^2: ";
        int terms;
        cin >> terms;
        myFactor.setA(terms);
        cout << endl;

        cout << "Please enter the coefficient for x: ";
        cin >> terms;
        myFactor.setB(terms);
        cout << endl;

        cout << "Please enter the constant term (c): ";
        cin >> terms;
        myFactor.setC(terms);
        cout << endl;

        // tell the user what they entered again before proceeding, however i do not ask if this is correct again
        cout << "The polynomial you entered has coefficients:\n" << "A: " << myFactor.getA()
     << "\nB: " << myFactor.getB() << "\nC: " << myFactor.getC() << endl;

     cout << "\nFactoring C (" << myFactor.getC() <<")......\n" << endl;
        // begin factoring with new numbers immediately.
        myFactor.cFactor(myFactor.getC());

    }


    // run the test factors program to determine what the proper factors to use are
    myFactor.testFactors();

    // this line just tells you what a and b are
    cout << "in (x+a)(x+b)\n" << "a is " << myFactor.getFactoreda()
     << "\nand b is " << myFactor.getFactoredb() << endl;

    // this block of if statements will spit out the correct syntax of (x+a)(x+b) depending
    // on whether one or both factors are positive or negative respectively.
    if (myFactor.getFactoreda() < 0 && myFactor.getFactoredb() > 0)
    {
        cout << "\n So your final form is (x" << myFactor.getFactoreda() << ")(x+" << myFactor.getFactoredb() << ") " << endl;
    }

    if (myFactor.getFactoreda() > 0 && myFactor.getFactoredb() < 0)
    {
        cout << "\n So your final form is (x+" << myFactor.getFactoreda() << ")(x" << myFactor.getFactoredb() << ") " << endl;
    }

    if (myFactor.getFactoreda() < 0 && myFactor.getFactoredb() < 0)
    {
        cout << "\n So your final form is (x" << myFactor.getFactoreda() << ")(x" << myFactor.getFactoredb() << ") " << endl;
    }

    if (myFactor.getFactoreda() > 0 && myFactor.getFactoredb() > 0)
    {
    cout << "\n So your final form is (x+" << myFactor.getFactoreda() << ")(x+" << myFactor.getFactoredb() << ") " << endl;
    }

    // delete dynamically allocated memory when program ends.
    myFactor.deleteArray();
    return 0;
}
Last edited on
well what i suggest is that factoring a quadratic expression is by finding the roots...............and the writing it in the form (x-a)(x-b) where 'a' and 'b' are the roots of the quadratic expression.....
Something in Factor.cpp doesn't sit right with me from lines 22-36.
You define a pointer to an array of size zero. And begin to fill it. This is in the heap so it shouldn't cause a nightmare with small numbers but this may be where your error lies.

You need to resize the array every time you add something or just define the arrays to be 20, and keep the counter as a means to know how many are filled.

So the code should something like:
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
numberOfFactors = 0;
    pdivisor = NULL;
    pfactors = NULL;
 
    // calculation phase
    for(int iii=1;iii<20;iii++) // note that 20 is arbitrary, for testing purposes I limited the factors up to 20, should be enough for now
    {
        if ((C % iii) == 0)
        {
			// Define new storage to fit the new factor.
			int *p = new int[numberOfFactors + 1];
			int *q = new int[numberOfFactors + 1];
			
			// Copy over the old data.
			for (int f = 0; f < numberOfFactors; ++f)
			{
				p[f] = pdivisor[f];
				q[f] = pfactors[f];
			}
			
			// Add the new value to the array
			p[numberOfFactors] = iii;
			q[numberOfFactors] = (C / iii);
			
			// Free up the old data;
			delete[] pdivisor;
			delete[] pfactors;

			// Set pdivisor and pfactors to new arrays to keep them.
			pdivisor = p;
			pfactors = q;
			
			// Increment the number of know factors by 1
            numberOfFactors++;
        }
    }


I don't know if this is going to fix all of the program but its a start to the memory error you're having.
I implemented what you recommended wolfgang, it seems to fix my issue when using a large negative number. I will be adding to the program and testing it more tonight and tomorrow but the preliminary results seem promising. As I was thinking about how I set it up last night before bed it occurred to me that the dynamic array declared with zero elements was probably the cause of the issue, I just couldn't figure out how to not lose the data if I declared the array inside the for loop. But! you have shown me how to do that, many many thanks! Here is the updated file if you care to look, maybe make sure I didn't make some more beginners errors:

EDIT: It's not really clear to me though why the call to delete the arrays at the end of main was causing the issue though. I see how the way that it is set up now is much better than the way I had it before but why would just the deletion of the arrays cause that error? I could understand if the error was tripped when the cFactor program ran, as it had a dynamic array of zero elements, it would make sense to me that it errors there but it didn't... any insight you could offer would be much appreciated.

EDIT2: So perhaps I am wrong on this line of thinking but I believe it would be wise to deallocate the memory allocated to the new arrays pd and pf before pd and pf go out of scope. Here is my line of thinking: at the top of the if statement *pd and *pf are allocated memory of size numberOfFactors+1, we then copy over the contents of pdivisor and pfactors respectively into pd and pf. We then store the new value found in the space "numberOfFactors" (which ever number we are on) of the new pd and pf arrays. We then delete (deallocate) the dynamic memory of pdivisor and pfactors and copy over the contents of pd and pf to pdivisors and pfactors and increment numberOfFactors.

At this point all the factors that are of interest are stored in the data members pdivisor and pfactor which do not go out of scope when the end of the if statement is reached, however, pd and pf were declared inside the if statement and thus pd and pf will be destroyed when the end of the if statement is reached but the dynamically allocated memory that pd and pf point to have essentially no scope. Does this not create a memory leak? I thought it would be wise to do 'delete[] pd; delete[] pf' after the incrementation of numberOfFactors but this borks the program completely. It gives me an error of the same kind as before but does not get past 'Factoring C (-60).........." so obviously I am not correct with this assumption. However I feel that I am only incorrect in the method that I attempt to deallocate the memory, not that it does not need to be deleted. If I am wrong about this being a memory leak (however small, I would like to start programming with good practices though and leaving memory leaks in place is definitely not a good practice) please tell me why it will not cause a leak. If it is unclear what I am talking about let me know and I will try to explain better. Any insights at all that anyone can offer is highly appreciated

EDIT3: Sorry for all the edits but I keep thinking about this problem, maybe I understand. The line 'pdivisor = pd; pfactors = pf;' means that the pointer pdivisor now POINTS to the address that the pointer pd pointed to (same thing for pfactors and pf) so that when pd and pf go out of scope the data member pdivisor and pfactors STILL point to the dynamic memory originally allocated by pd and pf and thus there is no memory leak because pointers to that memory still exist. Am I right or what? ;)

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "Factor.h"
using namespace std;

Factor::Factor(int A, int B, int C)
{

    a = A;
    b = B;
    c = C;

}

// function to factor the third term in a polynomial
void Factor::cFactor(int C)
{
    // we just want the positive factors of C, leave out -1
    // this will be tested for by the testFactors function
    if (C < 0)
    {
        C=-C;
    }
    numberOfFactors = 0;

    pdivisor = NULL;
    pfactors = NULL;
    // calculation phase
    for(int iii=1;iii<20;iii++) // note that 20 is arbitrary, for testing purposes I limited the factors up to 20, should be enough for now
    {
        if ((C % iii) == 0)
        {
            // define new storage to fit new factors
            int *pd = new int[numberOfFactors+1];
            int *pf = new int[numberOfFactors+1];

            // copy over old data
            for(int i=0; i < numberOfFactors; i++)
            {
                pd[i] = pdivisor[i];
                pf[i] = pfactors[i];
            }

            // add the new value to the array
            pd[numberOfFactors] = iii; // keep track of evenly divisable numbers
            pf[numberOfFactors] = (C / iii);// place the cooresponding factor in another array but the same column

            // free up data of old arrays
            delete[] pdivisor;
            delete[] pfactors;

            // set old arrays to new arrays with new data found
            pdivisor = pd;
            pfactors = pf;

            // increment number of known factors by 1
            numberOfFactors++;

        }
    }

    //termination phase
    cout << "\nFormat of results: \n\n" << "Factor on top\n" << "Divisor on bottom\n" << endl;

    for (int nRow=0; nRow < numberOfFactors; nRow++)
    {
        cout << pfactors[nRow] << "\t";
    }
    cout << endl;
    for (int nRow=0; nRow< numberOfFactors; nRow++)
    {
        cout << pdivisor[nRow] << "\t";
    }

    cout << "\n\nWith a total of " << numberOfFactors << " possible factors to use." << endl;
} // end c factorization function

// function to get the current value of A
int Factor::getA()
{
    return a;
}

//function to get the current value of B
int Factor::getB()
{
    return b;
}

// function to get the current value of C
int Factor::getC()
{
    return c;
}

// function to get number of factors
int Factor::getNumberOfFactors()
{
    return numberOfFactors;
}

void Factor::testFactors()
{
    cout << "\n\nTesting these factors.......\n" << endl;

    for(int iii=0;iii<numberOfFactors;iii++)
    {
        // testing with positive factors
        if(pdivisor[iii]+pfactors[iii] == b)
        {
            if(pdivisor[iii]*pfactors[iii] == c)
            {
                factoreda = pdivisor[iii];
                factoredb = pfactors[iii];
            }
        }

        // testing with negative
        if (-pdivisor[iii]+pfactors[iii] == b)
        {
            if(-pdivisor[iii]*pfactors[iii] == c)
            {
                factoreda = -pdivisor[iii];
                factoredb = pfactors[iii];
            }
        }

        // testing with both factors negative
        if (-pdivisor[iii]-pfactors[iii] == b)
        {
            if(-pdivisor[iii]*(-pfactors[iii]) == c)
            {
                factoreda = -pdivisor[iii];
                factoredb = -pfactors[iii];
            }
        }

    }
}

int Factor::getFactoreda()
{
    return factoreda;
}

int Factor::getFactoredb()
{
    return factoredb;
}

void Factor::setA(int A)
{
    a = A;
}

void Factor::setB(int B)
{
    b = B;
}

void Factor::setC(int C)
{
    c = C;
}

void Factor::deleteArray()
{
    delete [] pdivisor;
    delete [] pfactors;
}
Last edited on
I figured I would also attach the output to show that it did indeed fix it for c=-60

|-(bobby@Desktop)-(0)-(12:20 AM Fri Dec 23)->
|-(~/Cpp Projects/FactoringTool/bin/Debug)-(1 files, 52Kb)--> ./FactoringTool
The polynomial you entered has coefficients:
A: 1
B: 8
C: 15
Does this look correct?
1. Yes
2. No
2
Please enter the coefficient for x^2: 1

Please enter the coefficient for x: -4

Please enter the constant term (c): -60

The polynomial you entered has coefficients:
A: 1
B: -4
C: -60

Factoring C (-60)......


Format of results:

Factor on top
Divisor on bottom

60 30 20 15 12 10 6 5 4
1 2 3 4 5 6 10 12 15

With a total of 9 possible factors to use.


Testing these factors.......

in (x+a)(x+b)
a is -10
and b is 6

So your final form is (x-10)(x+6)

Topic archived. No new replies allowed.