I am writing a program in which there is a part that computes all the possible values for two vectors of string patterns, one like 1XX78X9X32X (11 digit) the other like 26XX (4 digit), using digits from 0 to 9, and that passes all these possible strings to another two vectors.
So I wrote 16 nested for loops, the outer one for vector, the 15 inner ones for every digit of the patterns (11+4).
But for some reason the loop is infinite and it never terminates. I could not discover why.
I would really appreciate it if you have any ideas and can help me, it is really urgent.
You can run through and comment out each, one by one along with their closing bracket if you wish to find the offending loop.
You could also use your debugger, and place breakpoints at each loop and step out in each, which will shoot you to the next one, after however many iterations of each loop.
Are all of your functions working?
Are you sure it NEVER ends? What is the size of that first vector?
I don't see any immediate problems, but I just glanced over your code.
If there are 7 Xes, there are 107 ways to fill them.
Are you going to put all the 107 combinations of those strings and put them in two vectors or have one vector with 105 variants of first string and another with 100 variants of the second?
Anyway, now you have 1015 instead of 107 or 105 + 102. I suggest that if you want to fill n gaps, you should take all numbers i from 0 to 10n and replace the nth gap with the nth digit of i.
I'm pretty sure that it never ends. Because I have written cout's between the loops and tried this with size 1 of the most outer for loop and with strings with no X's, it still never ends. But now I discovered that it goes into infinite loop when I switch vectors, after the first 11 for loops, the first string. But I still cannot see where to fix.
Well, now ı realized that it has nothing to do with the number of X's since I had the if's apart.
Probably problem is that it is too slow. But how to implement the code in another way??
//This is how I would do N-nested for loops, even when N is not a compile time constant.
//Granted, this will not always work.
constint N = 16;
int *a = newint[N]; //indexes
//Make sure they all start at 0 (or at whatever value you want)
for( int i = 0; i < N; i++ )
a[i] = 0;
//One loop to rule replace them all
while( a[N - 1] <= 9 ) {
//Do stuff, a[0] = inner-most loop index, a[N -1] = outer-most loop index
...
//increment
for( int i = 0; i < N; i++ ) {
a[i]++;
if( a[i] <= 9 )
break;
if( i < N - 1 ) //don't reset the outer most index, would cause infinite loop
a[i] = 0;
}
}
delete a;
if( a[i] <= 9 )
continue;
if( i < N - 1 ) //don't reset the outer most index, would cause infinite loop
a[i] = 0;
You need this so that the next element after [0,9] is [1, 0], but if you use integers 9+1=10 happens automatically.
Then the code would be
1 2 3 4 5 6 7
int maximum = 1000000;//107for(int i = 0; i < maximum; i++){
//since you probably need the indices separately,
int a[7], j = i;
for(int k = 0; k < 7; k++, j/=10) a[k] = j%10;
//do stuff.
}
Though I have to agree that Mathhead's solution is better as it is not restricted by the size of an integer and doesn't need divisions.
However you might want to remember this method should you ever be dealing with bits.
I could not figure out how to use the above algorithm for my code, for example where to enumerate the X's. Sorry, I'm a real newbie... :( But could you please show the algorithm on part of my code as an example? Then I may understand it better..
I just need to know how to implement:
I don't know if you could implement your code with my method easily exca. Basically my method turns
1 2 3 4 5
for( int a1 = 0; a1 <= 9; a1++ )
for( int a2 = 0; a2 <= 9; a2++ )
…
… for( int aN = 0; aN <= 9; aN++ )
//code to run
into the above, except //code to run goes in the while loop, and a1 == a[0], a2 == a[1], …, aN == a[N - 1]
There is no code "in between" the for loops there. However though, the if statements could be written to run inside the next loop, only on the first iteration (i.e. index == 0). However after converting to my style I don't know if it would be easier to read (probably not worth the effort.)
Are you sure you really need 16 nested loops for this code to run though?