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

标签 c arrays algorithm multidimensional-array position

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

X=2 且 array[]={A,B} 在此示例中,数组包含 2D 矩阵的维度 (A,B)。所以在2D引用系中的位置是:

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

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

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

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

我是初学者!!!

最佳答案

如果您有一个具有维度 Xn 和 Yn 的数组 DIM2[X,Y],您也可以将其表示为一维数组(如您所说)。

A[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;
    }

关于c - 多维数组中元素的位置 [ C ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38054781/

相关文章:

c - 使用 execlp() 运行 C 程序?

c++ - LDAP 连接 ldap_sasl_bind_s 给出断言

python - 数组中的 Numpy 条件乘法数据(如果为真乘以 A,则为假乘以 B)

java - 将 ArrayList<String> 数据转换/传输到 String[] 时出现不兼容类型错误

C/C++ 执行邻接 2 距离矩阵的最佳算法 [由 Floyd-Warshall 解决]

c++ - 使用 MSYS 构建 libcurl

c - sparc_do_fork() 究竟做了什么?

javascript - 简洁的javascript获取特定键的所有唯一值

javascript - 从找到匹配项的数组中删除对象时遇到问题

javascript - 如何检查数组元素是否匹配某些模式(例如 :XXXXYY)?