mandez
mandez

Reputation: 137

Recursive function crashing

I wrote this recursive function to calculate the terms of the sequence:

enter image description here

and to place them in a float array of maximum 1000 element, but this function is crashing as I run and input the float A and I don't see what's the problem in there.

  #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.: The integer i initial value is 0, and the two floats b and c initial value is 1.

Upvotes: 0

Views: 96

Answers (2)

autistic
autistic

Reputation: 15642

Even with your latest edit, the code didn't compile for me. It's possible that you were running a broken binary. Not to worry, though! I managed to fix it.

  1. Remove the <conio.h> include. You will rarely if ever need this non-standard header. Change getch to getchar.
  2. Change void main to int main. main returns int, not void.

Viola! http://ideone.com/zeAuP1

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

There is no explicit test to make sure i stays below 1000; your code assumes that the recursion will stop before that happens, but I see nothing to insure this.

Upvotes: 2

Related Questions