-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2Ddynamic_array.cpp
More file actions
38 lines (35 loc) · 862 Bytes
/
2Ddynamic_array.cpp
File metadata and controls
38 lines (35 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
int** dynamic2Darray(int rows, int cols){
//array containg the address of the array of pointers
int** arr= new int*[rows];
//array containing the address for each row of the original 2D array
//array of pointers storing addresses
//allocating memory for each row
for(int i=0;i<rows;i++){
arr[i] = new int[cols];
}
//actual 2D array
//initialising the array
int value=0;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
arr[i][j] = value;
value++;
}
}
return arr;
}
int main()
{
int rows, cols;
cin>>rows>>cols;
int** arr= dynamic2Darray(rows, cols);
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}