Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I set x is array contains several variables with are inputs of a evaluation function. Each variable has limit defined in xBoundary[index][min,max]. Each possible variable value has step size xStep[].

e.g. x[0] is between 0 and 10 since xBoundary[0][0]=0 and xBoundary[0][1]=10. xStep[0]=0.5 then x has possible value 0,0.5,1,1.5,.....9.5,10. How can I access all possible value of x? I think this is a n dimensional array where n = length of x.

The following is my code. It is infinite loop.

#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;
}
share|improve this question
    
For starters, 1. Format your code, 2. declare xBoundary as xBoundary[nx_SIZE][2]={{0,5},{2,5}}; 3. There is a variable demantion undeclared in your function –  brokenfoot Mar 20 at 6:46
    
Thanks. I edited the typo and format. @brokenfoot –  Dan Mar 20 at 6:58

2 Answers 2

Based on your requirement, this is just a sample loop of nxSize times. try the code below(I didn't test it, you might need some updates on it)

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");
}

the code above was based on nx_SIZE = 2 which is in your example, if nxSIZE is another value, you should intialize xBoundary correctly in order to make it work.

share|improve this answer
up vote 0 down vote accepted

I found the problem is caused by double xStep[nx_SIZE]={1};. This only assign xStep[0] value to 1 and rest are 0. The zero increment cause infinite loop.

Value of x is n dimensional array where 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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.