Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  ss
Language: C/C++
Code:
#include <stdio.h> /* For input and output */
#include <stdbool.h> /* For bool, true, false */

int bsearch(int a[],int n,int k)
{int first=0;
int last = n-1;
int mid;
int i;
int position=-1;
bool found=false;

for(i=0;i<n;i++)
{mid=(first+last)/2;
if(a[mid]==k)
    { found=true;
position=mid;
      }
else if(a[mid]>k)
     last=mid-1;
else
     first=mid+1;
}
return position;
}
const int n=6;
int main()
{
     int result=0;
     int x[n]={1,2,3,4,5,6};
     int key;
     printf("enter the key");
     scanf("%d",&key);
      result=bsearch(x,n,key);
     if(result==-1)
          printf("are u f k me");
     else
          printf("the key%d found in the element number %d ",key,result);

     system("pouse");
     return 0;
}
          
Comments: