Reputation: 467
Am trying to use recursion to print k length sets from given input. Example of what I want :
input:
3
a b c
output:
aaa
aab
aba
.
.
so on.
I am somehow getting garbage values!
int klength(char input[],char output[],int size, int k, int curr){
int i=0;
if(k<=0)
{
puts(output);
return 0;
}
for(i=0;i<size;i++)
{
output[curr]=input[i]; // **EDIT**
printf("current output %s\n",output);
klength(input,output,size,k-1,++curr);
printf(" after recursion %s]n,output);
}
}
Can someone please point me to my mistake?! I can't somehow find my mistake and am running a little short on time :\ Thanks in advance!
Upvotes: 0
Views: 50
Reputation: 126253
klength(input,output,size,k-1,++curr);
here you increment curr
each time in the loop, which means it increases rapidly and eventually overflows your output buffer. You want
klength(input,output,size,k-1,curr+1);
instead.
Upvotes: 3