Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions