#include#include void accept(int [],int); void display(int [],int); void search(int [],int); void main() { int a[20],n; clrscr(); printf("\n How Many Numbers You Want To Accept:"); scanf("%d",&n); accept(a,n); printf("\n Element Entered are: "); display(a,n); search(a,n); getch(); } void accept(int a[],int n) { int i; printf("\n Enter %d Numbers in ascending order\n",n); for(i=1;i<=n;i++) { printf("\n Enter Element %d :",i); scanf("%d",&a[i]); } } void display(int a[],int n) { int i; for(i=1;i<=n;i++) { printf("\t%d",a[i]); } } void search(int a[],int n) { int i,ele,start,mid,end,flag=0; printf("\n Enter The Number To Be Searched: "); scanf("%d",&ele); start=0; end=n-1; mid=(start+end)/2; while(start<=end) { if(ele==a[mid]) { flag=1; break; } if(ele>a[mid]) { start=mid+1; } if(ele<a[mid]) { end=mid-1; } mid=(start+end)/2; } if(flag==1) { printf("\n Number Found."); } else { printf("\n Number Not Found."); } } /*OUTPUT How Many Numbers You Want To Accept:4 Enter 4 Numbers in ascending order Enter Element 1 :5 Enter Element 2 :9 Enter Element 3 :22 Enter Element 4 :44 Element Entered are: 5 9 22 44 Enter The Number To Be Searched: 5 Number Found. How Many Numbers You Want To Accept:8 Enter 8 Numbers in ascending order Enter Element 1 :4 Enter Element 2 :8 Enter Element 3 :10 Enter Element 4 :12 Enter Element 5 :45 Enter Element 6 :47 Enter Element 7 :58 Enter Element 8 :65 Element Entered are: 4 8 10 12 45 47 58 65 Enter The Number To Be Searched: 45 Number Found. How Many Numbers You Want To Accept:5 Enter 5 Numbers in ascending order Enter Element 1 :4 Enter Element 2 :8 Enter Element 3 :9 Enter Element 4 :10 Enter Element 5 :12 Element Entered are: 4 8 9 10 12 Enter The Number To Be Searched: 2 Number Not Found. */
Write a C Program for implementing Binary Search method using Function.
Reviewed by
on
November 16, 2013
Rating: