usingnamespace std;
double** calloc_2d(longint l,longint m) //allocate a double matrix
{
double** a = (double**) calloc(l, sizeof(double*));
longint i;
for(i=0; i<l; i++)
{
a[i] = (double*) calloc(m, sizeof(double));
}
return a;
}
int main()
{
int size_i = 10; //number of ellements in 2-d array
int size_j = 20;
double** a;
a = calloc_2d(size_i,size_j); //a has been declared as a 2-d array and initialized as zero
return 0;
}
I want to generalize this to a 3-d array, but I'm having problems. The code for the 3-d array compiles but when I run the code it never finishes running. It's like it is stuck in a infinite loop. This is what I have:
You might want to try a more manageable way to store a multi-dimensional array. You can use the formula a=z*width*height+y*width+x (correct me if I'm wrong) to encapsulate it in a class as a 1d array. Something like this: