Hello everyone,
I have written a code for binary search for my class. My professor asked us to create it using bool, and the header function "bool binarySearch(int a[], int start, int end, int number)".
I am having some issues with it. it all seems fine until i get to part where i have to print the answer in my main function. I am not sure what's wrong. can you please help me? i'd really appreciate it
#include <iostream>
using namespace std;
bool binarySearch(int a[], int start, int end, int number)
{
int mid;
bool b=false;
if(start<=end)
{
b=true;
}
while (B)/>/>
{
b=false;
mid= (start+end)/2;
if (number == a[mid]) //if whatever is in index 3 is equal to the number entered
return mid;
else if (number>a[mid])
{ start=mid+1;
b=true;
}
else
{ end= mid-1;
b=true;
}
}
}
int main()
{
int a[]={5,10,23,34,45,55,66,78,81,98};
int number;
int answer;
cout<< "Enter a number: "<<endl;
cin>> number;
answer= binarySearch(a,0,9,number);
cout<< "you entered "<<number<<" located at index: "<< answer;
system("pause");
return 0;
}