#include <stdio.h>s
void main()
{
int c, first, last, middle, n, search, arr[20];
printf("Enter 20 integers\n");
for (c = 0; c < 20; c++)
scanf("%d",&arr[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = 19;
middle = (first+last)/2;
while (first <= last)
{
if (arr[middle] < search)
first = middle + 1;
else if (arr[middle] == search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);
getch();
}
void main()
{
int c, first, last, middle, n, search, arr[20];
printf("Enter 20 integers\n");
for (c = 0; c < 20; c++)
scanf("%d",&arr[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = 19;
middle = (first+last)/2;
while (first <= last)
{
if (arr[middle] < search)
first = middle + 1;
else if (arr[middle] == search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);
getch();
}
No comments:
Post a Comment