0

hope you doing well. so i have just started to do an assignment and the first thing i wanted to do was to create the two dynamic array. however, there is something wrong with the array i can't assign values to it. Here is the code:

void Room::memory(int **array){
int x,x2;
int count=0;
cout << "Array size? rows: columns: \n";
cin >> x >> x2;
array = new int*[x];

for(int i=0; i<x;i++){
    array[i]= new int[x2];
}
for(int i=0; i<x; i++){
    for(int j=0; j<x2; j++){

        array[i][j]=count;
        count++;
    }
}
 for(int i=0; i<x;i++){
    array[i]= new int[x2];
}
for(int i=0; i<x; i++){
    for(int j=0; j<x2; j++){
     cout<<   array[i][j]<< " | ";

    }
    cout << endl;
}
}

i always get the value 0 for my array. whether i use this line or not:

        array[i][j]=count;

i tied to compare my code with someone else and it is the same steps but it doesn't work for me.

class Room{

private:
int **array;

public:
void memory(int **array);    

};
  • You need to study answer here : http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new – vcp May 20 '16 at 03:44

2 Answers2

0

Why do you do

for(int i=0; i<x;i++){ array[i]= new int[x2]; } twice?

at the same time, please change void memory(int **array); to void memory();

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
0

while you crested the 2nd 2D dynamic array,you did not initialize it...And just printed it... In the 2nd 2D array you also need to initialize before printing the all index values....

    for(int i=0; i<x;i++){
        array[i]= new int[x2];
    }
//here should be the initialization
    for(int i=0; i<x; i++){
        for(int j=0; j<x2; j++){
         cout<<   array[i][j]<< " | ";

        }
        cout << endl;
    }
RahulCoffee
  • 56
  • 11