Question
C++ I am trying to build a template binary search function. I have the follwoing, but it keeps getting stuck in a continous loop. Can
C++
I am trying to build a template binary search function. I have the follwoing, but it keeps getting stuck in a continous loop. Can someone help me fix this code so that it runs.
#include
using namespace std;
template
cout << last; bool found = false; int midpoint = first + last/2;
while(first <= last && found != true){ if(a[midpoint] == item) { found = true; } else { if(item > midpoint) first = midpoint + 1; else last = midpoint - 1; } }
return found;
}
int main() { vector
nums.push_back(2); nums.push_back(4); nums.push_back(6); nums.push_back(8); nums.push_back(10); nums.push_back(12); nums.push_back(14);
binarySearch(nums, 2); binarySearch(nums, 3);
return -1; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started