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 :)
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
#include <iostream>
#include <cmath>

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] <<endl;
    v=i[a]; 
    //What allows for negative number factors
    cout << i[a]-2*i[a] <<endl;
    }
    else
    v=i[a];
if (c>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++
1
2
3
4
5
6
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.
1
2
int x=1;
int i[x];
Should be
1
2
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.