I'm trying to figure out how I can use recursion to do n-level nested for loops. For example, if n=3, there would be 3 'levels'
for(z=0;z<6;z++){
for(y=0;y<6;y++){
for(x=0;x<6;x++){
if (z+y+x==f){
//do something
}
}
}
}
and so on.
I can't seem to figure out how I would be able to place the if loop in the last for loop and how I can access the variables of previous for loops from the if statement. I know that the question of variable nested loops has been asked alot of times, and I have looked through all of them. But none seem to help me.
Could someone present an easy way of using recursion to achieve this, keeping in mind that I'm still a beginner in c++, to point me in the right direction?
The use case is as follows:
Write a program to input the number of dice m. The program will output the total number of possible cases, the number of possible cases for each possible n and the n with the highest probability. Note: only one input m is read in. n is computed by the program
Example if user enters m=2 then program should output
The total number of possible cases is 36.
The possibilities are
2 1
3 2
4 3
.
.
.
12 1
You could write it like this, but... I wouldn't. It's confusing code and doesn't give you any benefits. If you want it because your true use case has a high number of nested loops, consider just not doing that, instead; it's a serious design smell.
Live demo.
You are very vague about why you want this. For a starter a possible solution is to replace each for loop with a recursive function.
And in the end you can merge this all into one function. Nevertheless using recursion just because it is possible is never a good Idea.
I came across this earlier today and thought I might share the solution that I eventually came up with. I'm not sure what the policy is here regarding replying to old posts. I'm just going by the fact that I came across this question this morning, and this kind of thing would have been useful to me.
For efficiency, I've avoided recursion. Also, it doesn't use any specific c++ stuff - it will work fine on C as well.
We're trying to create N nested "for" loops. Instead of using
I'll be replacing i, j, ... with an array: i[0], i[1], ..., i[n-1].
Here's my solution:
There, that's all. Hopefully the comments make it clear what it's meant to be doing. I think it should be pretty efficient - almost as much as real nested for-loops. Most of the overhead is a one-off at the beginning, so this should be more efficient that using recursive functions etc (please correct me if I'm wrong on this point).
Hope it's useful to somebody one day.
Peace and love.
The basic structure of a recursive algorithm with multiple loops is as follows:
The setup for calling
recursiveLoops
from the top level requires two vectors - one for the indexes, and one for the number of iterations at each level. The example below sets up three nested loops, iterating 5, 6, and 9 times at each level:Here's an example in plain old C++. First I make a vector of the ranges for each dimension called
maxes
. if the sum of all indices are 2 then I print did something. In the example I loop z from 0 to 1, y from 0 to 2, x from 0 to 3You can for sure make this more neat.
Here goes:
result:
just count the depth for each recursion function, and count to
f
..if you really want you can use three different functions for x,y and z.