You are on page 1of 1

1. #include <conio.

h>
#include <stdio.h>
int nhap(int a[], int n)
{
int i;
for (i=0;i<n;i++)
{
printf ("Moi nhap phan tu thu %d: ",i+1);
scanf ("%d",&a[i]);
}
}
int tk(int a[], int n, int x)
{
int l=0,r=n-1,k;

while (l<=r)
{
k=(l+r)/2;
if (a[k]==x) return k;
if (x<a[k]) r=k-1;
else l=k+1;
}
return -1;
}
int main()
{
int a[50],n,x,c;
printf ("Moi nhap so phan tu cua mang: ");
scanf ("%d",&n);
nhap(a,n);
printf ("Nhap so can tim trong mang: ");
scanf ("%d",&x);

c=tk(a,n,x);
if(c==-1) printf ("Khong co phan tu nao trong day trung!");
else printf ("Phan tu thu %d co gia tri %d!",c,x);
getch();
return 0;
}