+ 1

Binary search

I wrote this C program for binary search but it is showing runtime error:- #include<stdio.h> int binary(int a[],int i,int n,int x) { if(n<1) return -1; int m = i + (n-i)/2; int l = n; if(a[m]==x) return m; if(a[m]>x) return binary(a,m+1,n,x); if(a[m]<x) return binary(a,i,m-1,x); } int main() { int a[10],x,i; printf("Enter sorted array: "); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("Enter number to be searched: "); scanf("%d",&x); int n = sizeof(a)/sizeof(a[0]); int b = binary(a,0,n-1,x); if(b!=-1) printf("Number is at %d position.",b); else printf("Number not found."); return 0; } please help me

22nd Nov 2017, 1:27 PM
Shantanu Shinde
Shantanu Shinde - avatar
0 odpowiedzi