Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include

using namespace std;

template bool binarySearch(vector &a, T item) { int first = 0; int last = a.size() - 1;

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;

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

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

Find the indicated term of each sequence. 1, 5/3, 7/3, . . . (25th)

Answered: 1 week ago

Question

Finding and scheduling appointments with new prospective clients.

Answered: 1 week ago