I had some free time today and I try to write a recursive method for your code.
It is not a clean code, could be better, but could be a start point.
It works for any combination of combo size and numbers size.
have a look.
Code:
int *pcombonum,*pcombo;
int totalLen=20; // Total numbers
int comboLen=4; // size of combo
void PrintOut(void)
{
for (int i=0; i < comboLen; i++)
printf("%d, ",pcombo[i]);
printf("\n");
}
// fill the combo with the two last digits
void fullfill(int len,int total,int *pbase)
{
int i,j;
int tot=total;
j=len-1;
if (j < 0)
return;
printf("\n");
for (i=0; i < total-len; i++)
{
pcombo[j]=pbase[i];
PrintOut();
}
tot--;
if ((tot-len)==0)
{
return;
}
if ((j-1) < 0)
return;
pcombo[j-1]=pbase[0];
fullfill(len,tot,&pbase[1]);
}
int main()
{
int i,j,k;
int *pcombonew= new int[totalLen];
int *pcombouse= new int[totalLen];
if (comboLen > totalLen)
comboLen=totalLen;
pcombo= new int[comboLen];
pcombonum= new int[totalLen];
for (i=0;i < totalLen ; i++) // First fillup of vector base;
pcombonum[i]=i+1;
for(j=0; j < totalLen-comboLen+1; j++)
{
int len=totalLen-j;
for (i=0;i < totalLen-j; i++)
pcombouse[i]=pcombonum[i+j];
for (k=0; k < len-comboLen+1; k++)
{
for (i=0;i < len; i++)
pcombonew[i]=pcombouse[i];
for (i=comboLen-3; i < len-k;i++)
pcombonew[i]=pcombouse[i+k];
for (i=0; i < len; i++) // Copy the first occurence
pcombo[i]=pcombonew[i];
fullfill(comboLen,len+1-k,&pcombonew[comboLen-1]);
}
}
printf("end recursive\n");
getch();
return 0;
}