Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

hey im having trouble with getting my binary search to find values above the mid

ID: 3656488 • Letter: H

Question

hey im having trouble with getting my binary search to find values above the middle...When i test the value of 100 here...it always comes back as a fail. can someone show me why. int binarySearch(int ar[], int valueSearchedFor) { int first = 0, last = 9, position = -1, middle; bool found = false; while ((!found) && (first <= last)){ middle = (last + first) / 2; if (ar[middle] == valueSearchedFor){ position = middle; found = true;} else if (ar[middle] < valueSearchedFor) { first = middle + 1;} else { last = middle + 1;} } return position; } . . . . . . int values[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; cout << "------ Section 1 ------ "; // testing Binary Search int result = binarySearch(values, 20); if (result == 1) { cout << "Section 1: Test Case 1 of 3 - pass "; } else { cout << "Section 1: Test Case 1 of 3 - fail "; } result = binarySearch(values, 12); if (result != -1) { cout << "Section 1: Test Case 2 of 3 - fail "; } else { cout << "Section 1: Test Case 2 of 3 - pass "; } result = binarySearch(values, 100); if (result != 9) { cout << "Section 1: Test Case 3 of 3 - fail "; } else { cout << "Section 1: Test Case 3 of 3 - pass "; }

Explanation / Answer

#include<iostream.h> #include<conio.h> #include<process.h> void main() { int ar[100],beg,mid,end,i,n,search; clrscr(); coutn; cout