I have a function that gets n and k to create all possible permutations of n, selects k, and although it works for most combinations like 5, select 3 or 3, choose 2, this is not for others like 4 choose 2 I need help finding and understanding the error. Thanks for watching.
Function:
void PermGenerator(int n, int k) { int d[] = {1,2,3,4,5,6,7,8,9}; sort (d, d+n); cout << "These are the Possible Permutations: " << endl; do { for (int i = 0; i < k; i++) { cout << d[i] << " "; if (i == k-1) cout << endl; } } while (next_permutation(d, d+n)); }
I am using the next_permutation function. cplusplus
When I try 4 to select 2, I should get 12 permutations, instead I get the following:
1 2 1 2 1 3 1 3 1 4 1 4 2 1 2 1 2 3 2 3 2 4 2 4 3 1 3 1 3 2 3 2 3 4 3 4 4 1 4 1 4 2 4 2 4 3 4 3
While 3 chooses 2, it works fine with 6 possible permutations:
1 2 1 3 2 1 2 3 3 1 3 2
source share