Write a C program called searchBinaryc that prompts the user

Write a C program called searchBinary.c that prompts the user for 10 positive integers entered in an ascending order and stor es them in an array of integers. The program then prompts the user again to search for a value s in the array that either does or doesn\'t exist in the array. Next the program considers the field in the middle of the array list[k] where k = n/2 . It then det ermines if the value of the middle field is greater, less, or equal to s . If it is equal to s , the value has been identified and the index location is found. If it is greater than s (the element being searched is less than the middle element of the array) then the search is repeated for the array starting from position 0 to k - 1 . If the value is less than s (the element being search is greater than the middle element of the array) the n the search is repeated for the array starting from position k+1 to n - 1 . This process of halving the array and looking at the middle element is repeated until the element is found or not located in any of the position tested. If a match occurs the index o f the matched element will be printed to the screen in a suitable text message. Otherwise, an error message will be printed to the screen indicating that the searched element does not exist among the entered values. The program then terminates

Solution

#include <stdio.h>
int main()
{
int m, firstele, lastele, mid,key, arr[10];
for (m = 0; m<10; m++)

scanf(\"%d\",&arr[m]);

printf(\"Enter value to find\ \");
scanf(\"%d\", &key);

firstele = 0; lastele = n - 1;
mid = (firstele+lastele)/2;

while (firstele <= lastele)
{
if (array[mid] < search)

firstele = mid + 1;
else if (array[mid] == search)
{
printf(\"%d found at location %d.\ \", search, mid+1);
break;
}
else lastele = mid- 1;   
mid = (firstele + lastele)/2;

}
if (firstele > lastele)
printf(\"Not found! %d is not present in the list.\ \", search);   
return 0;
}

Write a C program called searchBinary.c that prompts the user for 10 positive integers entered in an ascending order and stor es them in an array of integers. T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site