Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #define LEN 10 #define NOT_FOUND -1 int binary_search(int array[], int len, int key); int main(void) { int array[LEN] = {3, 12, 21, 23, 34,

#include

#define LEN 10

#define NOT_FOUND -1

int binary_search(int array[], int len, int key);

int main(void) {

int array[LEN] = {3, 12, 21, 23, 34, 37, 52, 88, 89, 99};

int key, position;

printf("Enter a positive number to be searched for: ");

if ((scanf("%d", &key) != 1 || key < 0)) {

printf("Please enter a positive integer. ");

return 1;

}

position = binary_search(array, LEN, key);

if (position != NOT_FOUND)

printf("%d is at location %d. ", key, position+1);

else

printf("Not found. ");

return 0;

}

int binary_search(int array[], int len, int key) {

int lower, upper, middle;

lower = 0;

upper = LEN - 1;

middle = (lower + upper) / 2;

while (array[middle] != key && lower <= upper) {

if (key < array[middle])

upper = middle - 1;

else

lower = middle + 1;

middle = (lower + upper) / 2;

}

if (array[middle] = key)

return middle;

else

return NOT_FOUND;

}

when I enter 35, the output is no "NOT FOUNF". How to correct the errors?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

What is a financial feasibility analysis?

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago