Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable nested for loops

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

like image 474
cortex Avatar asked Mar 04 '12 14:03

cortex


People also ask

Can nested for loops use the same variable?

In Python variables have function-wide scope which means that if two variables have the same name in the same scope, they are in fact one variable. Consequently, nested loops in which the target variables have the same name in fact share a single variable.

Can you put a for loop inside a variable?

Often the variable that controls a for loop is needed only for the purposes of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the for.

How do you do nested loops?

When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations before the outer loop can continue to its next iteration.

What is nested loop in C++?

A loop within another loop is called a nested loop. Let's take an example, Suppose we want to loop through each day of a week for 3 weeks. To achieve this, we can create a loop to iterate three times (3 weeks). And inside the loop, we can create another loop to iterate 7 times (7 days).


2 Answers

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

for(int i = 0; i<max; i++)
  for (int j = 0; j<max; j++)
    ...

I'll be replacing i, j, ... with an array: i[0], i[1], ..., i[n-1].

Here's my solution:

const int n = /*Insert N here: how many loops do you need?*/;
int i[n+1]; // if "n" is not known before hand, then this array will need to be created dynamically.
//Note: there is an extra element at the end of the array, in order to keep track of whether to exit the array.

for (int a=0; a<n+1; a++) {
  i[a]=0;
}

int MAX = 79; //That's just an example, if all of the loops are identical: e.g. "for(int i=0; i<79; i++)". If the value of MAX changes for each loop, then make MAX an array instead: (new) int MAX [n]; MAX[0]=10; MAX[1]=20;...;MAX[n-1]=whatever.

int p = 0; //Used to increment all of the indicies correctly, at the end of each loop.
while (i[n]==0) {//Remember, you're only using indicies i[0], ..., i[n-1]. The (n+1)th index, i[n], is just to check whether to the nested loop stuff has finished.

  //DO STUFF HERE. Pretend you're inside your nested for loops. The more usual i,j,k,... have been replaced here with i[0], i[1], ..., i[n-1].


  //Now, after you've done your stuff, we need to increment all of the indicies correctly.
  i[0]++;
  // p = 0;//Commented out, because it's replaced by a more efficient alternative below.
  while(i[p]==MAX) {//(or "MAX[p]" if each "for" loop is different. Note that from an English point of view, this is more like "if(i[p]==MAX". (Initially i[0]) If this is true, then i[p] is reset to 0, and i[p+1] is incremented.
    i[p]=0;
    i[++p]++; //increase p by 1, and increase the next (p+1)th index
    if(i[p]!=MAX)
      p=0;//Alternatively, "p=0" can be inserted above (currently commented-out). This one's more efficient though, since it only resets p when it actually needs to be reset!
  }
}

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.

like image 56
BugMeNot2013 Avatar answered Sep 25 '22 21:09

BugMeNot2013


The basic structure of a recursive algorithm with multiple loops is as follows:

void recursiveLoops(vector<int>& indexes, const vector<int>& endPerIndex, int currentIndex) {
    if (currentIndex == indexes.size()) {
        // This is where the real logic goes.
        // indexes[i] contain the value of the i-th index.
    } else {
        for (indexes[pos] = 0 ; indexes[pos] != endPerIndex[pos] ; indexes[pos]++) {
            // Recurse for the next level
            recursiveLoops(indexes, endPerIndex, pos+1);
        }
    }
}

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:

vector<int> indexes(3, 0);
vector<int> endPerIndex;
endPerIndex.push_back(5);
endPerIndex.push_back(6);
endPerIndex.push_back(9);
recursiveLoops(indexes, endPerIndex, 0);
like image 33
Sergey Kalinichenko Avatar answered Sep 21 '22 21:09

Sergey Kalinichenko