关于一个小程序的问题
初学C写个小程序,我感觉find函数最后那个return应该有,但是加上后结果就不对了,求解释#include<stdio.h>
#define N 5
int find(int a[],int x,int left,int right)
{
int mid;
if (left>right)
return(0);
else
{
mid=(left+right)/2;
if(x==a[mid])
return(mid+1);
else
{
if(x>a[mid])
find(a,x,a[mid]+1,right);
else
find(a,x,left,a[mid]-1);
}
return(mid+1);
}
}
void main()
{ int a[N],i,x,result;
printf("input a[%d]:\n",N);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
printf("input the number x:\n");
scanf("%d",&x);
result=find(a,x,0,N-1);
if(result==0)
printf("wrong\n");
else
printf("a[%d]=%d\n",result,x);
}