There are 10 numbers that are stored in an array in small to large order, and enter a number that requires the binary lookup method to find out that the number is the value of the first element in the array. If the number is not in the array, print "No this number".
Input:-12-8 12 24 45 46 56 58 68 78
Enter the data you want to find: 58
Output: 58 subscript is 7
Enter the data you want to find: 21
Output: "No this number"
#include "stdio.h"
void Chazhao (int a[])
{
int i,s,mid,high,low,flag=1,n=0;
printf ("Please enter the number to look for:");
scanf ("%d", &s);
for (i=0;i<10;i++)
if (S==a[i])
N=1;
if (n==0)
{
printf ("No such number \ n");
flag=0;
}
while (flag)
{
high=10;
low=0;
while (Low<=high)
{
Mid= (High+low)/2;
if (S==a[mid])
{
printf ("%d subscript is%d\n", s,mid);
flag=0;
Break
}
if (S<a[mid])
{
High=mid;
}
if (S>a[mid])
{
Low=mid;
}
}
}
}
void Main ()
{
int a[10];
int i,j;
for (i=0;i<10;i++)
scanf ("%d", &a[i]);
Chazhao (a);
}
2017/1/8 C Language Program Exercise D