【问题标题】:How to access n dimensional array using recursion如何使用递归访问 n 维数组
【发布时间】:2014-04-26 20:17:38
【问题描述】:

我设置 x 是数组,其中包含几个变量,它们是评估函数的输入。每个 变量在xBoundary[index][min,max] 中定义了限制。每个可能的变量值都有步长xStep[]。

例如x[0] 介于 0 和 10 之间,因为 xBoundary[0][0]=0 和 xBoundary[0][1]=10。 xStep[0]=0.5 然后 x 有可能的值 0,0.5,1,1.5,.....9.5,10。 如何访问 x 的所有可能值?我认为这是一个 n 维数组,其中n = length of x。

以下是我的代码。这是无限循环。

#include <stdio.h>

#define nx_SIZE 2                   // dimension of x

double x[nx_SIZE] = {0};                     // Initialize x
double xBoundary[nx_SIZE][2]={{0,5},{2,5}};  //Contains lower and upper boundary of x.
                                             //xBoundary[][0]=min
                                             //xBoundary[][1]=max
double xStep[nx_SIZE]={1};                   //Step size for variable in x.

void iterate(int dimension) {


  int i = dimension - 1;
  double currentX, upperLimit;

  if (i == -1){

    printf("x: %f %f \n", x[0], x[1]);
    //evaluate();

  } else {

    currentX = xBoundary[i][0];
    upperLimit = xBoundary[i][1];

    while (currentX < upperLimit){
      x[i] = currentX;
      iterate(i);
      currentX = currentX + xStep[i];
    } //End of while

  }
}

int main (){

  iterate(nx_SIZE);

  return 1;
}

【问题讨论】:

  • 对于初学者,1. 格式化您的代码,2. 将 xBoundary 声明为 xBoundary[nx_SIZE][2]={{0,5},{2,5}}; 3. 在您的函数中有一个变量 demantion 未声明
  • 谢谢。我编辑了错字和格式。 @brokenfoot

标签: c arrays recursion multidimensional-array


【解决方案1】:

我发现问题是由double xStep[nx_SIZE]={1}; 引起的。这仅将xStep[0] 值分配给1,其余为0。零增量导致无限循环。

x 的值是 n 维数组,其中n = length of x。

#include <stdio.h>

#define nx_SIZE 2                              // dimension of x

double x[nx_SIZE] = {0};                       // Initialize x
double xBoundary[nx_SIZE][2]={{0,5},{2,5}};    //Contains lower and upper boundary of x.
                                               //xBoundary[][0]=min
                                               //xBoundary[][1]=max
double xStep[nx_SIZE]={1,1};                   //Step size for variable in x.
int counter = 0;                               //Number of possible x

void iterate(int dimension) {

  int i = dimension - 1;
  double currentX;

  if (i == -1){
    printf("x: %f %f \n", x[0], x[1]);
    counter++;
  } else {

    for (currentX = xBoundary[i][0]; currentX <= xBoundary[i][1]; currentX += xStep[i]) {
      x[i] = currentX;
      iterate(i);
    }

  } //End of if
} // End of iterate

int main (){

  iterate(nx_SIZE);
  printf("counter: %d", counter);
  getchar();

  return 1;
}

【讨论】:

    【解决方案2】:

    根据您的要求,这只是 nxSize 次的示例循环。试试下面的代码(我没有测试,你可能需要一些更新)

    int i;
    for (i = 0; i < nx_SIZE; i++) {
        int v;
        for (v = xBoundary[i][0]; v <= xBoundary[i][1]; v += xStep[i]) {
            printf("%f", v);
        }
        printf("\n");
    }
    

    上面的代码是基于 nx_SIZE = 2 的,在你的例子中,如果 nxSIZE 是另一个值,你应该正确初始化 xBoundary 以使其工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-02
      • 2015-01-09
      • 1970-01-01
      • 2020-06-23
      • 2021-03-05
      • 2019-11-13
      • 2016-07-13
      • 2010-11-27
      相关资源
      最近更新 更多