Question: I have this nonrecursive function in c++ that generates all permutations of an integer array of size 4 maximum, I want to fix this function

I have this nonrecursive function in c++ that generates all permutations of an integer array of size 4 maximum, I want to fix this function to work for any size of N.

N can be any length, it is a parameter.

void generatePerm(int A[], int n) {

void generatePerm(int A[], int n) {

// determine total permutations possibe int total_permutations = 1; for (int i = 2; i <= n; ++i) { total_permutations = total_permutations*i; } int jdx = 1; int count = 0;

int perm[n]; for (int permutations = 0; permutations < total_permutations; ) {

for (int i = 0; i < n; ++i) { perm[i] = A[i]; }

int kdx = 0; while (kdx != total_permutations/n) { while (jdx != n-1) { for (int i = 0; i < n; ++i) { cout << perm[i] << " "; } cout << endl;

// swap elements int t = perm[jdx]; perm[jdx] = perm[jdx+1]; perm[jdx+1] = t;

// increment permutations kdx++; permutations++; jdx++; } jdx = 1; } count++; if (count == n) break; // swap array elements for new permutation int swap = A[0]; A[0] = A[count]; A[count] = swap; }

}

}

Please int array not string array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!