#define N 12 // number of elements to permute.
void QuickPerm(void)
{
unsignedint a[N], p[N+1];
registerunsignedint i, j, tmp; // Upper Index i; Lower Index j
for(i = 0; i < N; i++) // initialize arrays; a[N] can be any type
{
a[i] = i + 1; // a[i] value is not revealed and can be arbitrary
p[i] = i;
}
p[N] = N; // p[N] > 0 controls iteration and the index boundary for i
display(a, 0, 0); // remove comment to display array a[]
i = 1; // setup first swap points to be 1 and 0 respectively (i & j)
while(i < N)
{
p[i]--; // decrease index "weight" for i by one
j = i % 2 * p[i]; // IF i is odd then j = p[i] otherwise j = 0
tmp = a[j]; // swap(a[j], a[i])
a[j] = a[i];
a[i] = tmp;
display(a, j, i); // remove comment to display target array a[]
i = 1; // reset index i to 1 (assumed)
while (!p[i]) // while (p[i] == 0)
{
p[i] = i; // reset p[i] zero value
i++; // set new index value for i (increase by one)
} // while(!p[i])
} // while(i < N)
} // QuickPerm()
void display(unsignedint *a, unsignedint j, unsignedint i)
{
for(unsignedint x = 0; x < N; x++)
printf("%d ",a[x]);
printf(" swapped(%d, %d)\n", j, i);
return; // press any key to continue...
} // display()
or by moving the entire definition up to before you use it.
This code:
1 2 3 4 5 6 7 8 9 10
int main(){
void display(unsignedint *a, unsignedint j, unsignedint i)
{
for(unsignedint x = 0; x < N; x++)
printf("%d ",a[x]);
printf(" swapped(%d, %d)\n", j, i);
return; // press any key to continue...
}return 0;
} // display()
is a mess and indicates that you aren't actually familiar with how to write C or C++ code. Function definitions must not contain other function definitions. You've got a function, main, containing an entire definition of another function, display.
The structure of your code should be something like:
1 2 3 4 5 6 7 8 9 10 11
// Necessary header file includes
// Definition of display function
// Definition of Quickperm function
int main()
{
Quickperm();
return 0;
}