元素在多维数组中的位置[C]

时间:2016-06-27 13:06:14

标签: c arrays algorithm multidimensional-array position

我必须编写一个接收整数X(线性化位置的值)的函数和一个包含多维数组维度的数组,它必须在第二个数组中保存位置为X的元素的坐标在多维参考系统中。例如:

X = 2且array [] = {A,B},其中数组包含此示例中2D矩阵的尺寸(A,B)。所以二维参考系统中的位置是:

知道: X = x * B + y ----> x = X / B y = X%B ---->的位置[] = {X,Y};

因此将X解密为x和y很简单,因为它是2D矩阵的平庸情况,但我的程序必须处理N维矩阵(因此它必须将X解密为x,y,... ....,n)。

我的想法是应用我已经展示的算法,但即使我找不到可以处理通用N维矩阵的C代码(我也尝试编写递归函数而没有成功)。

有人能找到解决这个问题的方法吗? (提前谢谢!!!)

我是初学者!!!

1 个答案:

答案 0 :(得分:0)

如果你的数组DIM2 [X,Y]的尺寸为Xn和Yn,你可以将它(如你所说)表示为一个单一的数组。

然后将[x,y]映射到DIM1 [x + y * Xn]

DIM1的大小(Xn * Yn)

尺寸为Xn,Yn,Zn的3维数组B []可以用相同的方式映射:

B [x,y,z]将映射到DIM1 [x + y * Xn + z * Xn * Yn],DIM1必须能够保存(Xn * Yn * Zn)项目,
B [x,y,z,a]将映射到DIM1 [x + y * Xn + z * Xn * Yn + a * Xn * Yn *  锌

等等

对于通用N维数组,递归最好,其中100维的数组是99维数组的数组。如果所有尺寸都具有相同的尺寸,那将是相对简单的(写它,我还提到递归可以很容易地展开成一个简单的for循环,在下面找到它)

    #include <stdio.h>
    #include <math.h>
    #include <malloc.h>

    #define max_depth  5    /* 5 dimensions        */
    #define size      10    /* array[10] of array  */

    // recursive part, do not use this one
    int _getValue( int *base, int offset, int current, int *coords) {
        if (--current)
           return _getValue (base + *coords*offset, offset/size, current, coords+1);

        return base[*coords];
    }
    // recursive part, do not use this one
    void _setValue( int *base, int offset, int current, int *coords, int newVal) {
        if (--current)
           _setValue (base + *coords*offset, offset/size, current, coords+1, newVal);
        base[*coords]=newVal;
    }

    // getValue: read item
    int getValue( int *base, int *coords) {
        int offset=pow( size, max_depth-1);   /* amount of ints to skip for first dimension */
        return (_getValue (base, offset, max_depth, coords));
    }
    // setValue: set an item
    void setValue( int *base, int *coords, int newVal) {
        int offset=pow( size, max_depth-1);
        _setValue (base, offset, max_depth, coords, newVal);
    }

    int main() {
        int items_needed = pow( size, max_depth);

        printf ("allocating room for %i items\n", items_needed);
        int *dataholder = (int *) malloc(items_needed*sizeof(int));
        if (!dataholder) {
            fprintf (stderr,"out of memory\n");
            return 1;
        }
        int coords1[5] = { 3,1,2,1,1 };    // access member [3,1,2,1,1]
        setValue(dataholder, coords1, 4711);
        int coords2[5] = { 3,1,0,4,2 };
        int x = getValue(dataholder, coords2);

        int coords3[5] = { 9,7,5,3,9 };
        /* or: access without recursion: */
        int i, posX = 0;                           // position of the wanted integer
        int skip = pow( size, max_depth-1);        // amount of integers to be skipped for "pick"ing array
        for (i=0;i<max_depth; i++) {
            posX += coords3[i] * skip;             // use array according to current coordinate
            skip /= size;                          // calculate next dimension's size
        }
        x = dataholder[posX];

        return x;
    }