### Program Crash

I wrote a program to factor trinomials in the format "x^2 + bx + c" to "(x + a)(x + y) but it crashes after giving the factor outputs. Any help in fixing the crash is greatly appreciated :)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include using namespace std; int main() { int a=-1; int b; int c; int n; int m; int x=1; int factor; int v=0; int i[x]; int z=0; int poly; int num=0; cout << "Use the format x^2 + bx + c to input the following\nvariables so I can factor the trinomial for you :D\n"; cout << "Please input b: "; cin >> b; cout << "Please input c: "; cin >> c; if (c>0) {m=c;} if (c<0) {m=c-c-c;} while(c!=0) { if(m%c==0) { x++; a++; factor=m/c; i[a]=factor; } if (v!=i[a])//Prevents factorial repeats {cout << i[a] <0) {c--;} if (c<0) {c++;} }; a=x-1; if (b>0 && c>0) {do{poly=i[z]+i[a]; z++; a--;} while(poly!=b); cout << "(x + " << i[z] << ")(x + " << i[a] << ")\n";} if (b<0 && c<0) {do{ int poly=i[z]+i[a]; z++; a--;} while(poly!=b); cout << "(x - " << i[z] << ")(x - " << i[a] << ")\n";} else {do{ int poly=i[z]+i[a]; z++; a--;} while(poly!=b); cout << "(x + " << i[z] << ")(x - " << i[a] << ")\n";} system("pause"); return 0; }``````

P.S. My compiler is Dev C++
 ``123456`` ``````int poly; //... do{ int poly=i[z]+i[a]; z++; a--;} while(poly!=b);``````

you are creating new variable poly, which shadows previous, but check old value, which you never change inside loop.
 ``12`` ``````int x=1; int i[x];``````
Should be
 ``12`` ``````const int x=1; int i[x];``````

And you can see other error: you incrementin this varible, but array size will not increase because of this: you accessing out of bounds.
Last edited on
Thank you for pointing that out.
Line 13:
`int x=1;`
declares an int variable called x, value 1

Line 16:
`int i[x];`
declares an array of 1 place (since x = 1) --> This is strange, reflects perhaps some misunderstandings of what arrays are and how they behave.

Lines 39, 41, 42, 43, 45, 48, 56, 59, 62, 65, etc.
use variables to reach cells of the array. Remember the array was declared as having 1 cell? That's probably the problem. If in any case you end up evaluating an expression like:
`i[2]; i[10]; `
or anything beyond:
`i[0];`
you'll be in trouble.
I think my problem is that I thought if I increased the x variable afterwards it would increase the size of the array. Is that incorrect?
Yes it is. Arrays can't grow in size. You declare them as having a specific number of cells and that's what they get. The problem comes when you try to reach a place in memory beyond the array's borders: C++ will let you do that, and the program will crash (in almost every case). You are in charge of doing some bound checking before trying to evaluate an array cell.
What you are thinking of (an array capable of resizing during the execution of the program -runtime) is called vector. It's very similar, but it has a LOT more to give.

Last edited on
I really appreciate the help here :) Thank you
Topic archived. No new replies allowed.