开发者

C debugging variations generator

I would like to ask for help with debugging this simple piece of C code:

void generator (int place, char *array, int n, int lol){
int i;
char c[6]={'1','0','+','-','*','/'};

if(n==0){
printf("%s\n",array);
return;
}
for(i=0; i<6; i++){
    if (lol==0){
    开发者_开发问答    if (i>1) break;
        array[place]=c[i];
        lol=1;
        generator(place+1, array, n-1, lol);
    }
    if(lol==1){
        array[place]=c[i];
        if(i>1){
            lol=0;
            generator(n+1, array, n, lol);
        }
        else{ lol=1;
    generator(place+1, array, n-1, lol);}
    }
}}

The function is supposed to generate strings which contain n 1s and 0s separated by 0 to n-1 operators For example if n==3 then the output should be:

111
1+11
11+1
1+1+1
1*11
11*1
1*1*1
....
0/00
00/0
0/0/0
000

I'm a beginner programmer so would appreciate any tips.


Every call to the function is using the same array, and you "insert" symbols by over-writing an existing element of the array. Neither of these is what you want to happen.

Also, how are you calling the function? What is initially contained in the 'array'?


You should consider studying backtracking algorithms, since in my opinion this is a backtracking problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜