【发布时间】: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