I have an array of N values from 0 to k (0 <= k <= N), I need to generate all possible combinations of N values
void generate(int n, int k) {
int q = -1;
char res = '|';
int r;
int j;
for (j = 1; j <= n; j++) {
q = j / (k + 1);
r = j % (k + 1);
printf("%d %c", r, res);
}
}
int main() {
int n = 2;
int k = 2;
int i, nbr_comb;
nbr_comb = pow((k + 1), n); number of combinations
for (i = 0; i < nbr_comb; i++) {
generate(n, i);
printf("\n");
}
return (EXIT_SUCCESS);
}
for this test (N=2 K=2) I had those combinations
0 |0 |
1 |0 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
1 |2 |
you see that it begins to generate but it fixed at a point, I can't find why ! ?
Expected Examples: for n=2 k=2 n=3 k=2
0 0 0 0 0
0 1 0 0 1
0 2 0 0 2
1 0 1 0 0
1 1 1 0 1
1 2 1 0 2
2 0 1 1 0
2 1 1 1 1
2 2 1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
.....