i have built the following c++ program to create a Pascal's triangle. It works fine for a 5-row triangle but crashes when i try to make a 10-row triangle. It does show the 10-row triangle before crashing though.
at line 12 for (i=0; i<=level; i++)
the <= means i takes each value from 0 to level
But the array int ptriangle [level][level]; allows valid subscripts only in the range
0 to level-1
thus one extra row and one extra column outside the array are being used, thus corrupting some memory not owned by the program.
Also, the code uses a variable-length array which is not standard c++. level should be declared as const
Negative numbers - in this case it is because the value being stored exceeds the capacity of the integer type.
An int is usually a 32-bit binary value, with the highest-order bit used to represent the sign. You can squeeze a little bit more capacity by using unsigned integers
unsignedint ptriangle [level+1][level+1];
However I'd recommend here using type longlong which will usually be a 64-bit integer