Answer to Question #245654 in C for harry

Question #245654

[Binary search and overflow] Write a C program with a function that performs binary

search. Assume that the elements of the array are unsigned integers, and the following

elements are present along with other elements in the array: 4294967290, 4294967295,

10400.


1
Expert's answer
2021-10-02T07:24:18-0400
#include <stdion.h>

size_t bin_search(unsigned int *arr, size_t n, unsigned int target) {
  unsigned int m, l = 0, r = n;
  while (l < r) {
    m = (l + r) / 2;
    if (arr[m] == target) return m;
    if (arr[m] < targer) {
      l = m + 1;
    } else {
      r = m;
    }
  }
  return -1;
}

int main() { return 0; }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS