I have the following code that is doing a binary search based on array and value to look for that is passed to function. However, when I step into the function, the array has no values. Why is this happening? I have looked and I believe everything is correct in terms of passing the array.
#include <iostream>
using namespace std;
int binSearch(int [], int , int , int);
void main()
{
int a[10];
int low;
int high;
int searchValue;
for (int i = 0; i < 10; i++)
{
a[i] = i;
}
low = 0;
high = 9;
searchValue = 6;
int searchResult = binSearch(a, low, high, searchValue);
cout << searchResult << endl;
}
int binSearch(int a[], int low, int high, int searchValue)
{
int newHigh;
newHigh = (low + high) / 2;
if (low > high)
{
return -1;
}
else if (high == low)
{
if (a[high] == searchValue)
{
return high;
}
else
{
return -1;
}
}
else if (a[newHigh] < searchValue)
{
return binSearch(a, low, newHigh, searchValue);
}
else
{
return binSearch(a, newHigh, high, searchValue);
}
}