Dynamic Arrays (1 and 2D)

I am so confused about this concept. Can someone please explain to me (in complete idiot terms) how to

1. Declare a dynamic array
2. Declare a multidimensional dynamic array
3. Fill these arrays
4. Display these arrays

Thank you so incredibly much.
1. Declare a dynamic array
1
2
3
int* arrayName = new int[9001]; //it's over 9000!
//... eventually after you are finished with arrayName...
delete[] arrayName;

2. Declare a multidimensional dynamic array
1
2
3
4
5
6
7
8
9
int** multiDim = new int*[5];
for(int i = 0; i < 5; ++i) {
    multiDim[i] = new int[10]; //so each of the "rows" will have 10 ints
}
//... and later ...
for(int i = 0; i < 5; ++i) {
    delete[] multiDim[i];
}
delete[] multiDim;

3. Fill these arrays
4. Display these arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
//Filling and displaying are kind of the same thing
//Let's pretend I already created arrayName and multiDim
for(int i = 0; i < 9001; ++i) {
    arrayName[i] = i;
    std::cout << i << " ";
}
for(int i = 0; i < 5; ++i) {
    for(int j = 0; j < 10; ++j) {
        array[i][j] = 5*i + j;
        std::cout << array[i][j] << " ";
    }
    std::cout << "\n";
}
Last edited on
closed account (48T7M4Gy)
Also useful: http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
Topic archived. No new replies allowed.