0

Dump the loops, what you need is a recursive algorithm which would work for arbitrary lengths of arrays. One of the way is using 'Heap Permute'. Here is a sample program which you can easily adapt to use with C style strings:

`// Untested const int SIZE = 4; static int counter = 1; void swap(int arr[], int one, int two) { int tmp = arr[one]; arr[one] = arr[two]; arr[two] = tmp; } void print(int arr[]) { for(int i = 0; i < SIZE; ++i) { cout << arr[i] << " "; } cout << " -------> " << counter++; putchar('\n'); } void permute(int arr[], int size) { if(size == 1) { print(arr); } else { for(int i = 0; i < size; ++i) { permute(arr, size - 1); if(size % 2 == 1) swap(arr, 0, size - 1); else swap(arr, i, size - 1); } } } int main() { int myArr[SIZE] = {1, 2, 3, 4}; permute(myArr, SIZE); cin.get(); }`

I am wondering whether I am right or not, is it that one can call the function from its own code block , because I have been taught that one can call a function from any other function or from main().

I did not understand

```
void permute(int arr[], int size)
{
if(size == 1)
{
print(arr);
}
else
{
for(int i = 0; i < size; ++i)
{
permute(arr, size - 1);// function call from within the same function:(
if(size % 2 == 1)
swap(arr, 0, size - 1);
else
swap(arr, i, size - 1);
}
}
}
```