c语言,无法打破填充我定义的形状的函数中的循环
我被要求初始化一个具有定义形状的程序并创建一个函数来填充它, 我选择了一个三角形并计算出了如何填充它,问题是在最后,我的返回函数让我陷入了无限循环,我不知道如何摆脱它,该函数应该返回一个填充的数组,它确实这样做了,但我不知道最后如何打破循环。 该函数必须是递归的。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在那里对
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 theshape
intonshp
, and then returning a single element from it (using an out of bounds index, as pointed out already).