Help in making a Permutation function

Hello,

I need help in writing a function which declared as following:

void ReconstructPermutation(pointer* start, int perm[], int* permSize);

start: contain the address of the cell (which mean that it is a pointer to a pointer, this is why its type is pointer*).

the function update the array perm to contain the Permutation as numbers in the right order, and update the permutation length by permSize.

We can assume that perm is large enough to contain the permutation.

permutation example:
http://www.fastup.co.il/images/65369635.jpg

running example:

int perm[4], permSize, i;

pointer seq[4] = {NULL}; /* seq is of type array of pointers. */
/* Define the permutation with pointers: */

pointer* start = seq + 2; /* start is of type pointer to pointer. */

seq[2] = seq + 0; seq[0] = seq + 3; seq[3] = seq + 1;

ReconstructPermutation(start, perm, &permSize);

printf(">>> PERMUTATION IS: \n");

for (i = 0 ; i < permSize ; ++i)
printf("%d ", perm[i]);
printf("\n");

Output:
>>> PERMUTATION IS: 2 0 3 1

Thank you,
Dan
Last edited on
1) We don't do homework. Start the assignment yourself and feel free to ask specific questions instead of "Please write this function".

2) You haven't defined which permutation to output. A series of n numbers will have n! permutations.

Here's something that will probably help you to answer number 2... Look up lexicographical algorithms. This type of algorithm will guarantee that after n! iterations of a sorted algorithm, you will come across all possible permutations.

There is a function in std:: that will do this for you:
http://cplusplus.com/reference/algorithm/next_permutation/
Topic archived. No new replies allowed.