# Problem with recurrsion

• 09-01-2010
newbie123
Problem with recurrsion
Hi, All

I'm trying to call a function recursively. I tried this code and it doesnt seem to work correctly. The code compiles perfectly but it gives the wrong output. Can you guys suggest what might be wrong in here..

Code:

``` #include<stdio.h> #include<math.h> int rec(int[],int,int); void main() {                int i,a[18],ans;         //int a[8]={50,20,40,20,10,50,20,50};         printf("Enter the elements of array");         for(i=0;i<8;i++)         scanf("%d",&a[i]);         ans=rec(a,1,8);         printf("%d",ans); } int rec(int a[],int i,int j)         {                 int m,y,z;                 if(i>j) //                printf("Error");                 return 0;                 else if (i==j)                 //printf("%d",a[i]);                 return a[i];                 else                 {                         m=floor((i+j)/2);                         y=rec(a,i,m);                         z=rec(a,m+1,j);                         return y+2*z;                 }         }```
• 09-01-2010
pheininger
It may not be relavent, but what does (or should) rec( ) return if i>j or i==j?
• 09-01-2010
newbie123
if i>j then it shud return an error.. if it is equal to j, it shud return a[i]
• 09-01-2010
pheininger
Those are what you are sending to the output. The question is: What should the return value from the rec( ) fuction when you are outputing those values?
• 09-01-2010
newbie123
I had changed the return statements in the function.. Let me knw if it makes sense now?
• 09-01-2010
newbie123
I figured out where I went wrong.. Thanks for your help.. It is what made me think :)
• 09-01-2010
pheininger
Well now it does not have an obvoius mistake. I do not understand what it is supposed to calculate. I do not understand what the result produced means.