This is from a problem published on heakerearth.com
We need to create a 2 dimensional array and fill it.
for the creating of the array I've used:
1 2 3
for (int i = 0; i<= n;i++){
A[i] = newint [m];
}
when I let the program create a 2 x 2 array there is no problem Neither with a 2 x 3 or a 4 x 4. When I let the program create a 3 x 3 or a 3 x 2 array the filling breaks at value [i][j] [2][0]. So I think there must be something wrong there.
For testing I added a cout line in the creating part. Then I can create and fill a 3 x 3 array, but not the 3 x 2. I can still create and fill a 4 x 4 array.
1 2 3 4
for (int i = 0; i<= n;i++){
cout << i;
A[i] = newint [m];
}
Then I removed the cout << i; and changed the cout lines that asks for the input.
1 2 3 4 5
//not working code....
cout <<"give amount of rows \n";
cin >> N;
cout <<"\ngive amount of columns \n";
cin >> M;
Removed the \n at the end of the line because I do think they have got something to do with it.
1 2 3 4 5
/Working code....
cout <<"give amount of rows ";
cin >> N;
cout <<"\ngive amount of columns ";
cin >> M;
I'm missing some vital information about streams and I do not know what it is. Any tips, suggested reading (youtube) would be very welcome.
int main() {
//N, n, i = Rows, M = Columns
int N, M, n, m;
cout <<"give amount of rows ";
cin >> N;
cout <<"\ngive amount of columns ";
cin >> M;
n = N - 1;
m = M - 1;
int** A = newint*[n];
for (int i = 0; i<= n;i++){
A[i] = newint [m];
}
for(int i = 0; i <= n; i++){
for (int j = 0; j <= m; j++ ){
cout << "value [" << i << "][" << j <<"]: ";
cin >> A[i][j];
}
}
for (int i = 0 ; i <= n; i++){
for (int j = m; j >= 0 ; j--){
cout << A[i][j] << " ";
}
cout << '\n';
}
return 0;
}