Question: Help me fix this code (source.c) to print permutation of an array of characters without changing main.c and source.h main.c #include source.h /* printPermutation --

Help me fix this code (source.c) to print permutation of an array of characters without changing main.c and source.h

main.c

#include "source.h" /* printPermutation -- print a permutation of an array of char *'s */ static void printPermutation( int indices[], int nIndices, void *userArg) { int i; char **syms = userArg; for (i = 0; i < nIndices; i++) printf("%s ", syms[indices[i]]); printf(" "); } int main(int argc, char *argv[]) { genPerms(argc-1, printPermutation, argv+1); return 0; }

...................................................................

source.h

extern void genPerms(int nElems, void (*handlePerm)(int elems[], int nElems, void *userArg), void *userArg);

......................................................................

source.c

#include #include "gen_perms.h"

int level; enum { N_ELEM = 3, NOT_DONE = -1 }; int val[N_ELEM];

void recur(int k, int nElems) { int i;

val[k] = level; level++; if (level == nElems) { for (i = 0; i < nElems; i++) printf("%d ", val[i]); printf(" "); } for (i = 0; i < nElems; i++)

if (val[i] == NOT_DONE) recur(i, nElems); level--; val[k] = NOT_DONE; }

extern void genPerms(int nElems, void (*handlePerm)(int elems[], int nElems, void *userArg), void *userArg)

{

int i;

char **p = userArg; level = 0; for (i = 0; i < nElems; i++) val[i] = NOT_DONE; for (i = 0; i < nElems; i++) recur(i, nElems);

}

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!