Question: The following code snippet will print every 4 character long combination (without repetition) of the elements of an array. 

```
for (int i = 0; i < len; i++)
    for (int j = i + 1; j < len; j++)
        for (int k = j + 1; k < len; k++)
            for (int l = k + 1; l < len; l++)
                printf("%c%c%c%c\n", arr[i], arr[j], arr[k], arr[l]); 

```

My problem is I don't know how to extend this to a general function (ie print every N character long combination). How can I make a function do the same thing but be called like this:

```
combinationPrint(array, numberOfForLoops); // With other params if needed

```

Answer: 
Without recursion, you can use this (len = length of the array) :

```
int i, j, w, x;
for(i=0; i<pow(len,len); i++){ //n^n possibilities / combinaisons
    w = i;
    for(j=0; j<len; j++){ //Show the combinaison
        x = w%len; //We have juste to calculate the correct position with some modulos
        printf("%c", array[x]);
        w = w/len;
    }
    printf("\n");
}

```

For exemple with this implementation :

```
#include <stdio.h>
#include <math.h>

int main(){

    int array[] = {1,2,3};

    int len = 3;
    int i, j, w, x;
    for(i=0; i<pow(len,len); i++){
                w = i;
                for(j=0; j<len; j++){
                        x = w%len;
                        printf("%d", array[x]);
                        w = w/len;
                }
    printf("\n%d\n", i);
    }

}

```

You should have :

```
111
211
311
121
221 [...]
133
233
333

```
Well, instead of N loops - you just need one. Within this main loop you have to have just two inner loops for incrementing array of indices and for printing values:

```
int increment(int* index, int N, int len)
{
    for (int i = N - 1; i >= 0; --i)
    {
        ++index[i];
        if (index[i] < len)
            return 1;
        index[i] = 0;
    }
    return 0;   
}

```

So - the main loop:

```
    int index[N] = {};

    do
    {
        // print
        for (int i = 0; i < N; ++i)
            printf("%c", arr[index[i]]);
        printf("\n");
    } while (increment(index, N, len));

```