void allocateTriangle(int ** & twoDArray, int numRows);
void printTriangle(int ** twoDArray, int numRows);
void growToSquare(int ** & twoDArray, int numRows);
void printSquare(int ** & twoDArray, int numRows);
using namespace std;
int main(int argc, char ** argv){
srand((unsigned int)time(NULL));
int ** twoDArray;
int numRows = 10;
allocateTriangle(twoDArray, numRows);
cout << "******* Triangle array *******" << endl;
printTriangle(twoDArray, numRows);
growToSquare(twoDArray, numRows);
cout << "******* Sqaure array *******" << endl;
printSquare(twoDArray, numRows);
return 0;
}
void allocateTriangle(int ** & twoDArray, int numRows) {
twoDArray = new int*[numRows];
for (int x = 0; x < numRows; x++){
twoDArray[x] = new int[x];
for (int y = 0; y <= x; y++){
twoDArray[x][y] = rand() % 100;
}
}
}
void printTriangle(int ** twoDArray, int numRows){
for (int x = 0; x < numRows; x++){
for (int y = 0; y <= x; y++){
cout << setw(5);
cout << twoDArray[x][y];
}
cout << endl;
}
}
void growToSquare(int ** & twoDArray, int numRows) {
for (int x = 0; x < numRows; x++){
int *tempArray = new int[x];
*tempArray = *twoDArray[x];
--> delete [] twoDArray[x];
twoDArray[x] = new int[numRows];
for (int y = 0; y < numRows; y++){
if (y <= x)
twoDArray[x][y] = tempArray[y];
else
twoDArray[x][y] = rand() % 100;
}
}
}
void printSquare(int ** & twoDArray, int numRows){
for (int x = 0; x < numRows; x++){
for (int y = 0; y <= numRows; y++){
cout << setw(5);
if (y <= x)
cout << twoDArray[x][y];
else if (y - 1 == x)
cout << " ";
else
cout << twoDArray[x][y - 1];
}
cout << endl;
}
}
The arrow is pointing to where this program likes to crash. Giving me a Heap Corruption, I've spent the last 6 hours looking at this and other posts, I got nothing.
The main gets to growToSquare() and when I try to 'resize' the 2D array by deletion and allocation again, it stopes at deletion. I've commented out other lines to code to see if something else is triggering, nada.
Any words of advice? (I am using Visual studio 2013)
WhozCraig spotted it, in his comment above.
y <= x
should be y < x
, in multiple places in this code. – WhozCraigImagine that numRows
is 1
here, for the sake of simplicity. Follow the code logic in that simple case.
void allocateTriangle(int ** & twoDArray, int numRows) {
twoDArray = new int*[numRows];
for (int x = 0; x < numRows; x++){
twoDArray[x] = new int[x];
for (int y = 0; y <= x; y++){
twoDArray[x][y] = rand() % 100;
}
}
}
Here is the code that would execute, which assigns to a zero-length array.
twoDArray = new int*[1];
twoDArray[0] = new int[0];
twoDArray[0][0] = rand() % 100;
Memory has now been corrupted.
Plausible Solution for Triangle Allocation
Though there a about a half-dozen different ways to calculate the indices, this way is likely the easiest to understand.
void allocateTriangle(int ** & twoDArray, int numRows)
{
twoDArray = new int*[numRows];
for (int x = 0; x < numRows; x++)
{
twoDArray[x] = new int[x+1]; // NOTE: size
for (int y = 0; y <= x; y++)
twoDArray[x][y] = rand() % 100;
}
}
void printTriangle(int ** twoDArray, int numRows)
{
for (int x = 0; x < numRows; cout << endl, ++x)
for (int y = 0; y <= x; y++)
cout << setw(5) << twoDArray[x][y];
}
This allows you to keep your indexing (mostly) while sizing the allocations to appropriate lengths.
Growing a Triangle Into A Square
Similarly, the following will extend your triangle into a square. Note: you can invoke this on something already extended to a square. It will simply re-random-generate the right-half diagonal when doing so (and rather expensively at that).
void growToSquare(int ** & twoDArray, int numRows)
{
for (int x = 0; x < numRows; x++)
{
int *tempArray = new int[numRows];
for (int y=0; y<=x; ++y)
tempArray[y] = twoDArray[x][y];
for (int y=x+1; y<numRows; ++y)
tempArray[y] = rand() % 100;
delete [] twoDArray[x];
twoDArray[x] = tempArray;
}
}
void printSquare(int ** & twoDArray, int numRows)
{
for (int x = 0; x < numRows; cout << endl, ++x)
for (int y = 0; y < numRows; y++)
cout << setw(5) << twoDArray[x][y];
}
Output
Using the above methods, your output will look something like this. Obviously the random nature will result in different values, but the important thing is the bottom-left portion of the square retains the original triangle.
******* Triangle array *******
80
80 1
45 93 28
19 96 90 7
38 70 23 26 98
97 26 98 48 37 97
77 25 43 0 28 84 90
95 78 48 16 23 30 14 64
14 29 83 60 7 83 14 77 94
79 1 43 55 22 14 80 34 40 53
******* Sqaure array *******
80 10 62 17 62 94 62 47 87 3
80 1 56 67 56 91 85 51 25 8
45 93 28 9 42 57 95 56 19 42
19 96 90 7 46 67 42 77 53 73
38 70 23 26 98 53 22 34 69 3
97 26 98 48 37 97 8 18 53 55
77 25 43 0 28 84 90 7 82 43
95 78 48 16 23 30 14 64 94 33
14 29 83 60 7 83 14 77 94 75
79 1 43 55 22 14 80 34 40 53