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;
}
}