# Permutations Help

Hi All,

I am trying to have my program output all possible permutations of a certain array of integers. For instance if I have an array of 10 integers 1-10, I want it to output every permutation of numbers in sets of 3. Should look like:

111
112
113
114

121
122
123
124

131
132
133
134

221
222
223
224

etc making sure to cover all possible permutations.

So far I have gotten kind of close but I am still off. Every time my list reaches 10 in one column it resets as expected and increases the next column by one but then the right most column starts back at one digit higher than the last time it started counting in that column. Which means it is missing a whole bunch of permutations Example:

1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 1 10
1 2 2 ← Why doesn’t it start at 121
1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 2 8
1 2 9
1 2 10
1 3 3 ← Same here. Why doesn’t it start at 131
1 3 4
1 3 5
1 3 6
1 3 7
1 3 8
1 3 9
1 3 10
1 4 4 ← Same here. Why doesn’t it start at 141
1 4 5
1 4 6
1 4 7
1 4 8
1 4 9
1 4 10

Finally, it stops at 10 10 10, but I need it to run through all the permutations starting with 10. Example:

10 1 1
10 1 2
10 1 3
10 1 4

10 2 1
10 2 2
10 2 3
10 2 4

10 3 1
10 3 2
10 3 3
10 3 4

10 10 10

I know I am missing something(s) I just can’t see it yet. Any help would be appreciated. This is for a personal project and is not school work if that helps at all!

``````int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int r = 3;
int n = sizeof(arr) / sizeof(arr[0]);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
printCombination(arr, n, r);
}

void loop() {
// put your main code here, to run repeatedly:

}

int combinationUtil(int arr[], int data[], int n,
int i, int x, int r)
{
// Current combination is ready to be printed, print it
if (x == r)
{
for (int j = 0; j < r; j++)
Serial.print((String)data[j] + " ");
Serial.println("");
return;
}

for (int j = i; j < n ; j++)
{
data[x] = arr[j];
combinationUtil(arr, data, n, j, x + 1, r);
}

}

void printCombination(int arr[], int n, int r)
{
int data[r];
// Print all combination using temporary array 'data[]'
combinationUtil(arr, data, n, 0, 0, r);
}
``````

You never initialize your ‘data’ array inside printCombination() [side note: combination is an odd name for a function that prints permutations. not the same]

``````int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int r = 3;
int n = sizeof(arr) / sizeof(arr[0]);

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
printPermutations(arr, n, r);
}

void loop() {
// put your main code here, to run repeatedly:

}

void printPermutations(const int arr[], const int sz, const int r)
{
int counters[3];
bool done = false, incDone;
int incPos = 0;

for ( int i = 0; i < r; ++i ) {
counters[i] = 0;
}

while ( !done ) {
for ( int i = r-1; i >= 0; --i ) {
Serial.print(arr[counters[i]]);
Serial.print( " ");
}
Serial.println();
incDone = false;
incPos = 0;
while ( !incDone ) {
counters[incPos]++;
if ( counters[incPos] == sz ) {
counters[incPos++] = 0;
} else {
incDone = true;
}
if ( incPos == r ) {
incDone = true;
done = true;
}
}
}
}
``````