Using gdb, I found the problem is when
the left_values is not there because if I try
print left_values
right after declaring it, it tells meNo symbol "left_values" in current context.
after iterating through putting values into both right and left values, if I try, say
print right_values[1]
, it gives me some random large numbers.
Here's my code:
bool search(int value, int values[], int n)
{
if (n < 1)
{
return false;
}
// create two subtree of values
int isodd = 1;
if (n % 2 == 0)
{
isodd = 0;
}
int left_values[n/2];
int right_values[n/2];
if (isodd)
{
for (int i = 0; i < n/2; i++)
{
left_values[i] = values[i];
}
for (int i = n/2+1; i < n; i++)
{
right_values[i] = values[i];
}
}
else
{
for (int i = 0; i < n/2-1; i++)
{
left_values[i] = values[i];
}
for (int i = n/2; i < n; i++)
{
right_values[i] = values[i];
}
}
n /= 2;
// search value in values
while (n != 0)
{
if (values[n] == value)
{
return true;
}
else if (values[n] > value)
{
search(value, left_values, n);
}
else
{
search(value, right_values, n);
}
}
return false;
}