c语言,无法打破填充我定义的形状的函数中的循环

发布于 2024-10-09 04:22:41 字数 1434 浏览 3 评论 0原文

我被要求初始化一个具有定义形状的程序并创建一个函数来填充它, 我选择了一个三角形并计算出了如何填充它,问题是在最后,我的返回函数让我陷入了无限循环,我不知道如何摆脱它,该函数应该返回一个填充的数组,它确实这样做了,但我不知道最后如何打破循环。 该函数必须是递归的。

#include <stdio.h>

char Filler(char shape[][9],int start_colume,int start_line)
{
    int i,j,n,m;
    char nshp[5][9];

    for(i=start_line;i<5;i++)//condition for filling the function
    {
        for(j=start_colume;j<9;j++)
        {

            if(shape[i][j]!='*')
            shape[i][j]='|';
            if(shape[i][j]=='*')
                break;
        }
        Filler(shape,(start_colume)-1,(start_line)+1);//recursive rule
    }

    for(n=0;n<5;n++)//copying the created array into another array
    {
        for(m=0;m<9;m++)
            nshp[n][m]=shape[n][m];

    }

    return nshp[5][9];//the endless loop
}




void main ()
{
    char shape[5][9]={{' ',' ',' ',' ','*',' ',' ',' ',' ',},
                      {' ',' ',' ','*',' ','*',' ',' ',' ',},
                      {' ',' ','*',' ',' ',' ','*',' ',' ',},
                      {' ','*',' ',' ',' ',' ',' ','*',' ',},
                      {'*','*','*','*','*','*','*','*','*',}};
    int i=0,j=0;
    char nshap;
    for(i;i<5;i++)
    {
    for(j;j<9;j++)
      printf("%c",shape[i][j]);
      printf("\n");
    if(j=8)
        j=0;
    }

    nshap = Filler(shape,4,1);
    printf("%c",nshap);
    printf("\n");
}

有点白痴,但我正在调试这个函数一个小时左右,我尝试使用指针,但我真的很糟糕。

i was asked to initilize a program with a defined shape and to create a function to fill its,
i chose a triangel and worked out how to fill it, the problem is at the end ,my return function throws me into an endless loop and i have no idea how to get out of it, the function suppose to return a filled array,it indeed dose that but i have no idea how to break the loop at the end.
the function must be recursive.

#include <stdio.h>

char Filler(char shape[][9],int start_colume,int start_line)
{
    int i,j,n,m;
    char nshp[5][9];

    for(i=start_line;i<5;i++)//condition for filling the function
    {
        for(j=start_colume;j<9;j++)
        {

            if(shape[i][j]!='*')
            shape[i][j]='|';
            if(shape[i][j]=='*')
                break;
        }
        Filler(shape,(start_colume)-1,(start_line)+1);//recursive rule
    }

    for(n=0;n<5;n++)//copying the created array into another array
    {
        for(m=0;m<9;m++)
            nshp[n][m]=shape[n][m];

    }

    return nshp[5][9];//the endless loop
}




void main ()
{
    char shape[5][9]={{' ',' ',' ',' ','*',' ',' ',' ',' ',},
                      {' ',' ',' ','*',' ','*',' ',' ',' ',},
                      {' ',' ','*',' ',' ',' ','*',' ',' ',},
                      {' ','*',' ',' ',' ',' ',' ','*',' ',},
                      {'*','*','*','*','*','*','*','*','*',}};
    int i=0,j=0;
    char nshap;
    for(i;i<5;i++)
    {
    for(j;j<9;j++)
      printf("%c",shape[i][j]);
      printf("\n");
    if(j=8)
        j=0;
    }

    nshap = Filler(shape,4,1);
    printf("%c",nshap);
    printf("\n");
}

a little idiotic but I'm debugging this function for a hour or so, i tried to use pointers but i'm really bad with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2024-10-16 04:22:41

您在那里对 Filler() 进行了递归调用,但看起来您实际上并不需要它。你?递归终止条件是什么?

此外,nshp 局部变量似乎不太有用,因为您将 shape 复制到 nshp,然后从中返回单个元素(正如已经指出的,使用越界索引)。

You've got a recursive call to Filler() in there but it doesn't look like you actually need it. Do you? What's the recursion termination condition?

Also, the nshp local variable doesn't seem too useful because you are copying the shape into nshp, and then returning a single element from it (using an out of bounds index, as pointed out already).

柠北森屋 2024-10-16 04:22:41
In Pseudocode: 

begin main: 

Print data /*your shape*/
Filler (data,0,0);
Print data /* your filled shape*/ 

end main

Filler (data, row, col) {

if row valid then /* in range 0 to 4*/
   Filler (data,row +1,col) /*increment row & recurse */
else if col valid then /* in range 0 .. 8*/
   Filler (data,0,col+1) /*increment col & recurse*/
end if.

if row and col valid then
   if data(row,col) inside your shape then /* a more complex test, look for the star to the left and right of row */
      put fill character in data.
   end if
end if;

}
In Pseudocode: 

begin main: 

Print data /*your shape*/
Filler (data,0,0);
Print data /* your filled shape*/ 

end main

Filler (data, row, col) {

if row valid then /* in range 0 to 4*/
   Filler (data,row +1,col) /*increment row & recurse */
else if col valid then /* in range 0 .. 8*/
   Filler (data,0,col+1) /*increment col & recurse*/
end if.

if row and col valid then
   if data(row,col) inside your shape then /* a more complex test, look for the star to the left and right of row */
      put fill character in data.
   end if
end if;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文