Question: How would I implement a function with its arguments changing in a loop?

i.e. I would like to be able to implement the function in the form:

```
f( A[0], A[1], A[2] );
f( A[2], A[0], A[1] );
f( A[1], A[2], A[0] );
f( A[0], A[1], A[2] );
f( A[2], A[0], A[1] );
f( A[1], A[2], A[0] );

```

I got as far as being able to create the numbers in the correct place with the program below, tested using the printf() function:

```c
#include <stdio.h>

int main()
{
    int i, j, k, l;

    for(i = 0; i < 2; i++)
    {
        for( j = 0; j < 3; j++ )
        {
            for( k = 0; k < 3; k++ )
            {
                l = k - j;
                if( l < 0)
                    l += 3;
                printf("%d", l);
            }
            printf("\n");
        }
    }
}

```

Answer: 
If I understand your question correctly, all you need is:

```
int i;
for (i = 0; i < NUM_CALLS; i++)
    f(A[(2 * i) % 3], A[(2 * i + 1) % 3], A[(2 * i + 2) % 3])  

```
In the example given, it appears that you wish to loop through three (3) permutations of A[0], A[1] and A[2].

The simplest way, which might not be what you are looking for is to do ...

```
for (i = 0; i < NUM_ITERATIONS; i++)
    {
    f (A[0], A[1], A[2]);
    f (A[2], A[0], A[1]);
    f (A[1], A[2], A[0]);
    }

```

Someone else, I believe has already listed an algorithm using the modulus operator '%' so I will skip that. You may also wish to use a predefined array. Consider ...

```
ArgumentArray[6] = {A[0], A[1], A[2], A[0], A[1]};

for (i = 0; i < NUM_ITERATIONS; i++)
    {
    for (j = 0; j < 3; j++)
        {
        f (ArgumentArray[j], ArgumentArray[j+1], ArgumentArray[j+2]);
        }
    }

```

Granted, the above does not give you the same order of calling f() that you posted in your question, but it may be helpful in giving you new ideas about which way you want to proceed.

Hope this helps.