Home > Enterprise >  Multi-dimensional tables into one big table
Multi-dimensional tables into one big table

Time:11-17

For a project in C i need to work on a table that contains x differents 2 dimensionnals tables. However, i don't know how to create one, and how to call only one value [i,j] of one table into my general table [x,y] --> x and y are the coordinates of a table. this is an example of a small version of my table Could you please help me to understand how to call my values to then use them in my for or if conditions, or is it imposssible to work on such tables in C? Thank You

I tried to create a small version of two 3×3 tables into one 1×2 table. But even that did not work. I already know how to work on an x×x table.

I tried to also represent what i will have to work on with a little drawing on paint drawing. I hope this will help. My goal is the same, learn how to call just one element of every "intern array" that i will have (one the drawing for example calling X, X1, X2, ..., Xn)

CodePudding user response:

for multi-dimensional table you'd want to stack a few layers of pointers similarly to how dimensions stack in multi-dimensional algebra. the basic idea is something like this: for each dimension you want to higher tier pointer.

// #include <stdlib.h>
typedef int any_type;
unsigned index = 0;
unsigned count = 3;

// a point, 0 dimension
any_type d0; 
d0; // use item like this

// a vector of points, 1 dimension
any_type* d1 = (any_type*)malloc(sizeof(any_type) * count);
d1[index]; // use items like this

// a table of vectors, 2 dimension
any_type** d2 = (any_type**)malloc(sizeof(any_type*) * count);
for (int i = 0; i < count;   i)
{
    // a vector, 1 dimension
    d2[i] = (any_type*)malloc(sizeof(any_type) * count);
}
d2[index][index]; // use items like this

// a table of tables, 3 dimension
any_type*** d3 = (any_type***)malloc(sizeof(any_type**) * count);
for (int i = 0; i < count;   i)
{
    // a table of vectors, 2 dimension
    d3[i] = (any_type**)malloc(sizeof(any_type*) * count);
    for (int j = 0; j < count;   j)
    {
        // a vector, 1 dimension
       d3[i][j] = (any_type*)malloc(sizeof(any_type) * count);
    }
}
d3[index][index][index]; // use items like this

also after malloc you need to free the pointers in reverse order

CodePudding user response:

So you need an array that contains a matrix M*M in each of its cells, and each of the cells of the matrix eventually contains a double.

Note that the actual structure of the matrix isn't important when writing the program, you just need to count the dimensions you need and create the corresponding array. So an array of square matrices or a square matrix of arrays would still be written as a 3D array.

What you need to keep in mind is that in your case, the first dimension is the actual array (having 1 row and N columns as you claimed), the second dimension is the number of rows of the "internal matrix" and the third is the columns of that "internal matrix".

If you don't need to do particular operations on the array with functions, the answer is just allocating it automatically with

double array[width][height][depth];

And initialize it with a simple loop like

for (int i=0; i<width; i  ){
        for (int j=0; j<height; j  ){
            for (int k=0; k<depth; k  ){
                array[i][j][k] = 0.0; //any value
            }
        }
}

In this case, to access one element of the "internal matrix", fix the i value to the index of that matrix in the original array, then work with the other two dimensions as you would normally for a 2D array. For example, if you need to work on the second matrix, you could loop through the items like this:

for (int j=0; j<height; j  ){
        for (int k=0; k<depth; k  ){
            array[1][j][k] = 0.0;
        }
}

So you are working on the second cell of the array (hence i=1) and [j,k] would be the double you are working with. So the first element of the third row, in this second matrix would be accessible with array[1][0][2]

(Comment if you need more help)

  • Related