cb1295
cb1295

Reputation: 753

Array empty when passed to function

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);
    }
}

Upvotes: 2

Views: 3012

Answers (3)

stakri
stakri

Reputation: 1397

The array is fine. main() should return int in C++ and the binary search algorithm needed correction.

#include <iostream>
using namespace std;

int binSearch(int [], int , int , int);

int 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;
    return 0;
}

int binSearch(int a[], int low, int high, int searchValue)
{
    int mid;

    mid = (low + high) / 2;

    if (low > high)
    {
        return -1;
    }

    if (a[mid] == searchValue)
    {
        return mid;
    }    
    else if (a[mid] < searchValue)
    {
        return binSearch(a, mid+1, high, searchValue);
    }    
    else
    {
        return binSearch(a, low, mid-1, searchValue);
    }
}

Upvotes: 1

Idan Yehuda
Idan Yehuda

Reputation: 560

just change int binSearch(int a[], int low, int high, int searchValue) to

int binSearch(int* a, int low, int high, int searchValue)

Upvotes: 0

Peter Clark
Peter Clark

Reputation: 2943

When you pass an array into a function it is said to "decay" into a pointer. Arrays in C++ are second class citizens and any time you pass them somewhere they are automatically converted into a pointer to the first element. This means that when you inspect the "array" inside the function, you're actually viewing the value of the first element (since an the array decays to a pointer to the first element).

One way you can inspect it as an array at this point is to add it to the watch window (right click -> Add Watch, or type the variable name in watch window) and put a comma next to it along with the size of the array.

For example typing a, 10 in the watch window while in your bin_search function would display the first 10 elements pointed to by a.

Upvotes: 4

Related Questions