How to create an n amount of 'for' loops without writing all of them out?

Say I want to exhaust all the possible combinations of 10 variables to find all the solutions to an equation with 10 variables.

I could write out 10 'for' loops, but it would take a lot of space in the code and of course would take a lot of time. Another reason why I want to know this is because it could possible allow me to change the amount of for loops just by changing a number.

I.e., how can I contract the following code into a simple, short form?

1
2
3
4
5
6
7
8
9
10
11
12
for (a[0]=0;a[0]<=9;a[0]++)
 for (a[1]=0;a[1]<=9;a[1]++)
  for (a[2]=0;a[2]<=9;a[2]++)
   for (a[3]=0;a[3]<=9;a[3]++)
    for (a[4]=0;a[4]<=9;a[4]++)
     for (a[5]=0;a[5]<=9;a[5]++)
      for (a[6]=0;a[6]<=9;a[6]++)
       for (a[7]=0;a[7]<=9;a[7]++)
        for (a[8]=0;a[8]<=9;a[8]++)
         for (a[9]=0;a[9]<=9;a[9]++)
          if (...)
           cout << ... << endl;
1
2
3
for (int i = 0; i <= 9; ++i)
    for(a[i] = 0; a[i] <= 9; ++a[i])
        /* Code Here */


Last edited on
NoXzema. Seemingly your code would check one array element at a time. The loop would go like: 'put i=0. Check all a[0] from 0 to 9. Now put i=1. Check all a[1] from 0 to 9, etc.'

What I want the code to do is to check all 10 array elements at a time. As I said, imagine a 10 variable equation with the variables a[0],..,a[9]. I want to search for a combination of the 10 variables that would give me a solution to the equation.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

template < int N >
void print_it( int (&a)[N], int from_pos = 0 )
{
    if( from_pos == N )
    {
        for( int v : a ) std::cout << v << ' ' ;
        std::cout << '\n' ;
    }

    else
    {
        for( int i = 0 ; i < N ; ++i )
        {
            a[from_pos] = i ;
            print_it( a, from_pos+1 ) ;
        }
    }
}

int main()
{
    int array[3] {} ;
    print_it(array) ;
}

http://coliru.stacked-crooked.com/a/c517a9b4f85ec6b4
JLBorges. I get the following error:

range-based 'for' loops are not allowed in C++98 mode


It appears on the line for (int v : a) std::cout << v << ' ';

JLBorges. I get the following error:

range-based 'for' loops are not allowed in C++98 mode


It appears on the line for (int v : a) std::cout << v << ' ';


You need to compile with C++11;
Or use a classic for-loop:
1
2
// for (int v : a) std::cout << v << ' ';
for( int i = 0  ; i<N ; ++i ) std::cout << a[i] << ' ';


I presume that you realize that it is NP: O( NN )
JLBorges (or anyone else). So how would I write my exact code in your proposed shortened form?
What code? This?
1
2
          if (...)
           cout << ... << endl;

Do you see the use of recursion?

The example by JLBorges shows that lines 8-9 see the array 'a' to have specific enumeration of values on each iteration. Your code would thus be there.

Take a look at this:
http://www.cplusplus.com/forum/general/135491/#msg724044

Look especially at the looper function which calls itself within a for loop to create more for-loops
Topic archived. No new replies allowed.