I am working with a 2D array in a VS2010 console application. I am allocating the array as such:
Thing::Thing(int _n, bool _control){
m = _n;
control = _control;
thisArray = new int*[m];
for(int ii = 0; ii < m; ii++){
thisArray[ii] = new int[m];
for (int jj = 0; jj < m; jj++){
thisArray[ii][jj] = 0;
}
}
if (control == true){
int num = 1;
for (int jj = 0; jj < m; jj++){
for ( int ii = 0; ii<m; ii++){
if ((jj == (m-1)) && (ii == (m-1))){
std::cout << "inserting " << 0 <<
"at[" << ii << "][" << jj << "]" << std::endl;
thisArray[ii][jj] = 0;
std::cout << thisArray[ii][jj] << std::endl;
} else{
std::cout << "inserting " << num <<
"at[" << ii << "][" << jj << "]" << std::endl;
thisArray[ii][jj] = num++;
std::cout << thisArray[ii][jj] << std::endl;
}
}
}
pi.x = m-1;
pi.y = m-1;
}
}
then I attempt to display it through
void Thing::display(){
int x = 0;
int y = 0;
for( ; y < m; y++){
for( x = 0; x < m; x++){
if (Point(x, y) == pi){
std::cout << "[ ]";
}
std::cout << "[" << thisArray[x][y] << "]";
if ( x == m ){
std::cout << std::endl;
}
}
}
}
but it seems like it is only displaying the first dimension as output when I pass in 4, and true looks like this:
inserting 1 at[0][0]1
inserting 2 at[1][0]2
inserting 3 at[2][0]3
inserting 4 at[3][0]4
inserting 5 at[0][1]5
inserting 6 at[1][1]6
inserting 7 at[2][1]7
inserting 8 at[3][1]8
inserting 9 at[0][2]9
inserting 10 at[1][2]10
inserting 11 at[2][2]11
inserting 12 at[3][2]12
inserting 13 at[0][3]13
inserting 14 at[1][3]14
inserting 15 at[2][3]15
inserting 0 at[3][3] 0
[1][2][3][4]
, and when I add the array to my watch list the system shows the pointer to pointer to a single value, and then will only show me the first element, and not all of the elements.
it throws no run time access errors, or anything like that, but when I try to display the array it only does the first row. Note: I have been asked to use an actual 2D-array of ints, and am not allowed to use a pre-written library, or a single dimensional lie.
edit: added additional output info. edit2 (resolved): in display changed the second for loop to be
for (x = 0; x < m; x++)
when holding values external to a for loop insure that they are reset for circular iteration.