Enumerate and return all combinations of choices of k over n elements in a two-dimensional array from a recursive function in C

advertisements

I am using a recursive function (from one of the posts here) to enumerate all combinations of selecting k items out of n. I have modified this function to save and return the enumerated combinations in a 2-dimensional array (which is passed to the function as arrPtr). I call this recursive function in a for loop (from main) for different values of k (k from 1 to n-1) to generate all the combinations for any value of n and k. Now, with 'count', being defined as static integer, the function generates all the combinations for k=1 and then goes to k=2, but then stops at one point. The reason is that I'm using the variable 'count' as an index for rows in arrPtr. Since it is a static variable, it does not reset to 0 when the function is called for the other rounds (k=2,3,4 etc.). So it results in access violation for arrPtr after a certain point. When I remove 'static' for 'count', it generates all the combinations for different values of k, but only the last combination in each round is saved in arrPtr (again due to removing 'static'). How can I save each generated combination in a row in arrPtr so I can get (and return) all of the combinations saved in one place pointed to by arrPtr at the end?

I tried to pass the index for rows in arrPtr to the function using pass by reference (passing the address of the variable) but that gets into trouble when the recursive function calls itself. I searched a lot and found similar topics here (e.g., returning arrays from recursive functions), but they are mostly for other programming languages (I only use C; not even C++). I have spent many many hours on solving this and really need help now. Thank you in advance.

int** nCk(int n,int loopno,int ini,int *a,int **arrPtr, int k)
{
static int count=0;

int total; // equal to the total number of combinations of nCk
int i,j;

total = factorial(n)/(factorial(n-k)*factorial(k)); 

loopno--;
if(loopno<0)
{
    a[k-1]=ini;

    for(j=0;j<k;j++)
    {
    printf("%d,",a[j]);
    arrPtr[count][j]=a[j];
    }
    printf("count =%d\n",count);

    count++;

    return 0;
}

for(i=ini;i<=n-loopno-1;i++)
{
    a[k-1-loopno]=i+1;
    nCk(n,loopno,i+1,a,arrPtr,k);
}

if(ini==0)

return arrPtr; // arrPtr is in fact an array of pointers, where each pointer points to an array of size k (one of the combinations of selecting k out of n elements

else
return 0;
}


what i understand is

you want to calculate the combination for any value of n and k in nCk,

define a factorial() function outside and define a combi() function ... which calculates Combination value of n and k variables

both function before defining the main() function... that way you can avoid declaration and then defining (i mean avoid extra lines of code).

here is the code for combi() function

function combi(int n , int k){
int nFact, kFact, n_kFact, p;
int comb;
nFact=factorial(n);
kFact=factorial(k);
p=n-k;
n_kFact=factorial(p);
comb= nFact / ((n_kFact) * kFact);
return comb;
}

you can call this function in your main function .... use for loop to store the combination value for relative n and k .... thus you will get what you need .... also pass pointer or

&array[0][0]

i.e. starting address for the array... so that you can access that array anywhere in the program.

hope this may help you. thanks