递归函数崩溃

时间:2015-05-16 09:13:52

标签: c recursion

我编写了这个递归函数来计算序列的术语:

  

enter image description here

并将它们放在最大1000个元素的float array中,但此功能在我运行并输入float A时崩溃,我不知道问题是什么在那里。

  #include<stdio.h>
  #include<math.h>

  void triple_root(float B[1000],int i,float A,float b,float c){ 
      float x;
      x = 0.333*((A/(b*b))+(1/c));
      B[i] = x;

      if(fabs(x-b)<=0.00001|| i==999)
          puts(" ");
      else triple_root(B,i+1,A,x,b); 
  }

  int main(){
      float A[1000],b;
      int i;

      scanf("%f",&b);
      triple_root(A,0,b,1,1);

      for(i=0;i<1000;i++){
          printf("%f\n",A[i]);
      }
      getchar();
  } 

P.S。:整数i初始值为0,并且两个浮点bc初始值为1.

2 个答案:

答案 0 :(得分:2)

没有明确的测试来确保print "OK: Completed in %s %s|time=%s"%(delta, host, seconds) 保持在1000以下;你的代码假设递归会在发生之前停止,但我没有看到任何保证这一点。

答案 1 :(得分:1)

即使使用了最新的编辑,代码也没有为我编译。您可能正在运行损坏的二进制文件。不过不用担心!我设法解决了它。

  1. 删除<conio.h>包含。你很少需要这个非标准的标题。将getch更改为getchar
  2. void main更改为int mainmain返回int,而不是void
  3. 中提琴! http://ideone.com/zeAuP1