2. Show the error messages that you get. In general be explicit about what you want or need or what you are facing. For example, I don't bother in even looking at code that is not within code tags, and much less if I don't have a description of what I'm looking for. Others may work similarly.
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>
usingnamespace std;
bool BinarySearch(int a[256], int t)//* binary search algorithm
{
int size = sizeof(a)/sizeof(int);
cout << size;
int low = 0;
int high = size - 1;
while (low <= high)
{
int i = (low + high)/2;
if (t == a[i])
{
returntrue;
}
elseif(t < a[i])
{
high = i- 1;
}
else
{
low = i + 1;
}
returnfalse;
}
}
int main()//* Add integers to an array
{
int array[256];
for(int i=0;i<256;i++) {
cout <<"Input for position number " << i+1
<<"\n";
cin >>array[i];
cout <<"Add another integer?(Y/N) ";
char repeatans;
cin >>repeatans;
if (repeatans == 'N'){
break;
}
}
cout << "Number to search for: " << endl;//* Search for integer in array
int target;
cin >> target;
bool search_result;
search_result = BinarySearch(array[256], target);//* error: invalid conversion from ' int ' to ' int* '
if(search_result =! 0)
{
cout << "Integer was found";
}
else
{
cout << "Integer was not found";
}
}
gives me following errors:
|52|error: invalid conversion from 'int' to 'int*'|
|52|error: initializing argument 1 of 'bool BinarySearch(int*, int)'|
|53|warning: suggest parentheses around assignment used as truth value|
||=== Build finished: 2 errors, 1 warnings ===|
You're trying to pass array[256] into the function BinarySearch. That's wrong. Here, look:
array[0] is the first element of the array. It's an int. Right? So, array[256] is another int, which is actually off the end of your array. Not only are you trying to pass an int to a function that expects an int*, you're passing an int that doesn't even exist.
Change this bool BinarySearch(int a[256], int t)//* binary search algorithm
to bool BinarySearch(int a[], int t)//* binary search algorithm
And pass to the function like this: search_result = BinarySearch(array, target);//* error: invalid conversion from ' int ' to ' int* '
Not like this: search_result = BinarySearch(array[256], target);//* error: invalid conversion from ' int ' to ' int* '
And I think your warning is that there is no return statement if the while loop isn't true: