N number of nested for loops

Hey Guys,
I want to create an N number of nested loops, for any value for N inputted by the user. If N = 4, then

1
2
3
4
5
6
7
8
9
10
11
12
13
for(blah blah blah)
{
     for(blah blah blah)
     {
          for(blah blah blah)
          {
               for(blah blah blah)
               {
               
               }
          }
     }
}


Any idea of how this can be done ?
I tried using recursive functions, but that didn't work properly.
create a recursive function (aka.. it calls itself within the function)

1
2
3
4
5
6
void rec_for(int num_of_loops, int some_test_case){
    for(int i = 0; i < some_test_case; i++)
                rec_for(num_of_loops - 1, some_test_case - 1);


}
Last edited on
is it not like this?
1
2
3
4
5
6
void rec_for(int num_of_loops, int some_test_case){
    for(int i = 0; i < num_of_loops; i++)
                rec_for(int num_of_loops - 1, int some_test_case)


}


as your code wont stop
Last edited on
i tried that originally, but got a stack overflow (aka it didn't stop)
1
2
3
4
5
6
7
8
9
10
11
void rec_for(int num_of_loops, int some_test_case){
    for(int i = 0; i < some_test_case; i++)
    {
                if(num_of_loops < 1) break;
                else{
                rec_for(num_of_loops - 1, some_test_case);
                 cout << num_of_loops << " " << endl;
                 }
                }

}
you are correct... i just saw the problem

1
2
3
4
5
6
7
8
9
10
11
void rec_for(int num_of_loops, int some_test_case){
    cout << num_of_loops << " " << some_test_case << endl;
    for(int i = num_of_loops; i > 0; i--)
    {

                
                rec_for(num_of_loops - 1, some_test_case);
                 
}
}




Last edited on
Topic archived. No new replies allowed.