Salam,

My mission is to program a cube(given its dimensions (x,y,z)).
The cube will be represented in a struct, which I'll choose what data members it should contain.
I need to implement the allocation function(which actually creates the matrix) and some other functions.
The most important function is the setter function, which receives the bounds of an inner cube, and change the value of the inner cube to a given value.

I did an implementation of all of this, but I don't think it is efficient.
because I have this function:

void Setter(Struct Matrix * matrix, int xfirst, int xlast,
        int yfirst, int ylast, int zfirst, int zlast, int value) {
    int i,j,k;
    for(i=xfirst;i<=xlast;i++)
        for(j=yfirst;j<=ylast;j++)
            for(k=zfirst;k<=zlast;k++)
                matrix->m[i][j][k]=value; //m is defined in the struct: int ***m;
}

I will be very happy to get some ideas about some data structures or any tips that improves my program.

Recommended Answers

All 3 Replies

According to what i understood, you have a huge cube and you need to initialise the smaller cubes inside it, with their (x, y, z). Am i right?

A more elaborate explanation would be helpful.

Ok.. I'll try to better explain it.
I built a struct which represents a 3D array, lets say:

typedef struct MyCube {
    int ***array;
    int x;
    int y;
    int z;
} MyCube;

and I want to allocate memory for a cube, fill it with zeros.
Then I may need to change a "sub-cube" in it. The user will give me the sub cube and a value.. my mission is to set this sub-cube with the given value.

I did the implementation using a for, for, for loops.
I think this is very not efficient.
So I'm looking for another way to represent the cube.
By 2D array.. another fields.. don't know what. :(

Well, you can use a one dimensional array. But the number of computations would remain the same.

struct Cube {
    int x;
    int y;
    int z;
    int *subCubes;
};

for (i = 0 to Number_of_SubCubes) {
   /* initialize values */
}

eg: 3x3x3 cube, i varies from 0 to 26. But, the number of computations would remain the same. So, this in no way is a better solution, except that you can use a single loop that does the same job.(in the same time)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.