Home > OS >  Multiple Nested For-loops without knowing the number of for-loops
Multiple Nested For-loops without knowing the number of for-loops

Time:02-02

how would I write n nested for loops without knowing what n is? For example, how would i write this using recursion or another method:

for (int i = 0; cond1; i  ){
    for (int j = 0; cond2; j  ){
        for (int k = 0; cond3; k  )
        ...
            for (int l = 0; cond_N; l  ){
                if (.....) break;
            }
        }
    }
}

Here, there are n loops with some condition (not necessarily the same condition for each variable) and I'm not sure how to transform this into code using recursion without knowing what n is. Thanks!

CodePudding user response:

Is this what youre trying to do? cond provides the N different conditions (i.e. loop-variable-dependent bool function) and foo introduces recursion:

#include <iostream>

bool cond(int cond_which, int current_loop_variable) {
    switch (cond_which) {
    case 0:
        return current_loop_variable < 5;
    case 1:
        return current_loop_variable < 3;

    /* more... can be hella complicated, related with states, whatever */

    default:
        return false;
    }
}

void foo(int we_may_call_it_the_meta_loop_variable) {
    for (int i = 0; cond(we_may_call_it_the_meta_loop_variable, i);   i) {
        foo(we_may_call_it_the_meta_loop_variable   1);
        std::cout << "in loop " << we_may_call_it_the_meta_loop_variable << ", i = " << i << std::endl;
    }
};

int main() {
    foo(0);
    return 0;
}

Clearly this is not an infinite recursion.

CodePudding user response:

Here is a code with a recursive loop function. Right now the condition is the same for each variable: it must be lower than MAX. You could change it by passing to loop an additional array of function pointers that implement your conditions, i.e., a function that takes as argument an integer (a loop variable) and returns 0 or 1.

#define MAX 3

int
loop
(int * indexes, /* stores loop variables i, j, k, ... */
 int no_indexes /* number of loop variables */
 ) {
  int i;

  /* print the current values of loop variables for debugging */
  printf("handle iteration ");
  for(int i = 0; i < no_indexes; i   ) {
    printf(" %d", indexes[i]);
  }
  printf("\n");

  /* increment the last index variable and if reached MAX, reset it,
     increment the previous one and do the same for the previous
     one and so on */
  indexes[no_indexes - 1]   ;
  i = no_indexes - 1;
  while(i > 0 && indexes[i] == MAX) {
    indexes[i] = 0;
    indexes[i - 1]   ;
    i --;
  }

  /* recursive call if we have not terminated (i.e., if the first
     index variable is < to MAX) */
  if(indexes[0] != MAX) {
    loop(indexes, no_indexes);
  }
}


void
main
() {
  /*  test the function to simulate three nested loops  */
  int indexes[3] = { 0, 0, 0 };
  loop(indexes, 3);
}

Also note that the recursion is not at all required here since the whole body of the function could be placed in a while loop instead.

  •  Tags:  
  • Related