Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8) (15 pts) Complete the program below so that it prints out all the permutation

ID: 3751798 • Letter: 8

Question


8) (15 pts) Complete the program below so that it prints out all the permutations of 0,1,2,SIZE-1 such that the absolute value of the difference between each pair of adjacent numbers in the permutations is 2 or greater. For example, when SIZE 4, the code would print out: 1 3 0 2 2 0 3 1 the only 2 permutations such that the absolute value of the difference between each pair of adjacent terms is 2 or greater #include tinclude #defin SIZE 4 void printPerms (int perm, int used, int k, int n) void print (int perm, int n) int main) int perm [SIZE], used [SIZE], 1; for (i=0; íSIZE; i++) used [i] = printPerms (perm, used, 0, SIZE) return 0; 0; void printPerms (int perm, int used, int k, int n) if ( ) print (perm, n); int i; for (i-0 i

Explanation / Answer

#include <stdio.h>

#include <math.h>

#define SIZE 4

void printPerms(int perm[], int used[], int k, int n);

void print(int perm[], int n);

int main() {

int perm[SIZE], used[SIZE], i;

for (i=0; i<SIZE; i++) used[i] = 0;

printPerms(perm, used, 0, SIZE);

return 0;

}

void printPerms(int perm[], int used[], int k, int n) {

if (k == n) print(perm, n);

int i;

for (i=0; i<n; i++) {

if (!used[i]) {

if ( k == 0|| abs(perm[k-1]-i) >= 2) {

used[i] = 1;

perm[k] = i;

printPerms( perm, used, k+1, n);

used[i] = 0;

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote