[SOLVED!!] CUBE, dinamyc 3D array with diferent dimenzions

did u ever bother your self on how to make 3D array where second and third dimension will be of different size?
well here is an example on how to do that.
if you take a brief look you may conclude that, it's possible to create no matter how many different sized dimensions!
(the most important part of code is commented)
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 main() {
    int first, second, third;
    cout << "enter first dimension for MASTER->";
    cin >> first;
    int*** master = new int**[first];
    for(int i = 0; i < first; ++i) {
        cout << "enter second dimension for MASTER->" << i+1 << " dimension?:";
        cin >> second;
        master[i] = new int*[second];
        for(int j = 0; j < second; ++j) {
            cout << "enter third dimension for MASTER->" << i+1 << "->" << j+1 << " dimension?:";
            cin >> third;
            ++++third; //incrase third dimension to store data about dimension length's
            master[i][j] = new int[third];
            master[i][j][0] = second; //hold the value of second dimension
            master[i][j][1] = third; //hold the value of third dimensioNS for current second dimension.

            for(int k = 2; k < third; ++k)
                master[i][j][k] = 0; //initialize all the values to whatever
        }
    }
    cout << "\nlist of members:\n";
    for(int i = 0; i < first; ++i)
        for(int j = 0, sec = master[i][j][0]; j < sec; ++j) //second dimension is not always the same!!
            for(int k = 2; k < master[i][j][1]; ++k) //third dimension is not always the same as well!!
                cout << "MASTER->" << i+1 << "->" << j+1 << "->" << k-1 << ". member-> " << master[i][j][k] << endl;

    for(int i = 0; i < first; ++i) {
        for(int j = 0, sec = master[i][j][0]; j < sec; ++j) //second dimension is not always the same!!
            delete [] master[i][j];
        delete [] master[i];
    }
    delete [] master; // now it's time to kill last standing pointer
    return 0;
}


say thanks if you like my hack.
Topic archived. No new replies allowed.