1. Hello I have a c++ question I am doing this problem i already made the code b
ID: 3680002 • Letter: 1
Question
1. Hello I have a c++ question I am doing this problem
i already made the code but it doesn't run properly. it compiles just fine but when my program is running it crashes after It i input the value it asks me. I have been at this for hours
can someone please help me fix my code below. Keep in mind i am a beginner this is my first c++ course.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool binarySearch(const vector<int>& v, int k)
{ int midpoint = 0;
int lowest = 0;
int highest = v.size()- 1;
midpoint = (highest+lowest)/2;
while (lowest <=highest)
if ( k == v[midpoint] )
{
return midpoint;}
else if (k > v[midpoint])
{
lowest = midpoint+1;
}
else if (k < v[midpoint])
{
highest = midpoint - 1;
}
return -1;
}
int main()
{
int c=0;
vector<int> v(6);
for(int i=0; i<=v.size(); i++)
{
v[i] = i+3;
}
cout << "please enter a number you want to search for"<<endl;
cin >> c ;
binarySearch(v,c);
if (binarySearch)
cout<< "The number exists in the vector"<<endl;
else
cout << "The number does not exist int the vector"<<endl;
return 0;
}
Explanation / Answer
bool binary_search(const vector& sorted_vec, string key) { size_t mid, left = 0 ; size_t right = sorted_vec.size(); while (left sorted_vec[mid]){ left = mid+1; } else if (keyRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.