0

So I have a program here that's supposed to do the very simple job of searching through an Array of 100 integers to find a specific key. The linearSearch function in this program is supposed to be recursive, so I have to call the function from within itself. The program compiles fine, but for some reason only returns the first element 0, in the array no matter what search key you put in. What am I not seeing? Thanks.

#include <iostream>
using namespace std;

int linearSearch(const int[], int, int);

int main()
{
    const int arraySize = 100; //size of array  
    int a[arraySize]; //create array a
    int searchKey; //value to locate in array a

    for(int i = 0; i < arraySize; i++)
            a[i] = 2 * i; //create data

    cout << "Enter integer search key: ";
    cin >> searchKey;

    //attempt to locate search key in array a
    int element = linearSearch(a, searchKey, arraySize);

    //display results
    if(element != -1)
               cout << "Found value in element: " << element << endl;
    else
               cout << "Value not found" << endl;

    system("pause");   
}

//linearSearch Function ****Returns 0 as index all the time *************
int linearSearch(const int array[], int key, int sizeOfArray)
{
    static int index = 0;

    //if Out of range, return -1 (Value not found)
    if(index >= sizeOfArray){return -1;}

    //if array value = key, array index is returned
    if(array[index] == key){return index;}

    //if array value is not equal to key, add to index and call func again                
    if(array[index] != key)
    {
                    index++;
                    linearSearch(array, key, sizeOfArray);                
    }   
}

I'm taking the correct approach by declaring index as static, right?

~Thanks a lot, you guys were quick and a HUGE help. =)

6
  • No, a static index is almost certainly a bad idea. It's effectively a global variable which never gets reset. Commented Apr 1, 2013 at 14:34
  • 'Am I taking the right approach?' Definitely not, it should be a parameter. Commented Apr 1, 2013 at 14:34
  • No. It should be passed as a value parameter. Also, your function has no determinate return value on the tail-recurson resolution, so this stands no chance of working as is. Commented Apr 1, 2013 at 14:35
  • Surprisingly your code runs fine and gives correct results for me. Commented Apr 1, 2013 at 14:37
  • 1
    @stardust_ the behavior is undefined, since the function is being exited without a return. Commented Apr 1, 2013 at 14:40

4 Answers 4

4

Your mistake is that

if(array[index] != key)
{
    index++;
    linearSearch(array, key, sizeOfArray);                
}

should be

if(array[index] != key)
{
    index++;
    return linearSearch(array, key, sizeOfArray);                
}

You don't return any value when the key is not equal.

Plus the static index is bad, as has already been said.

Sign up to request clarification or add additional context in comments.

2 Comments

The whole if line can go as well.
Technically he doesn't have to return index there. Since it is static right now. As long as there is no code after that statement.
3

I'm taking the correct approach by declaring index as static, right?

No. Rather, index should be an argument to the recursive function.

Due to the static state, the function is hard to test and is not thread-safe. Furthermore, it inadvertently keeps state between invocations, meaning that if you were to call it a second time, it wouldn't work (since index would still have the old value).

Comments

1

You can also change your search function to the following without using static local variable index.

int linearSearch(const int array[], int index, int key, int sizeOfArray)
{
   //if Out of range, return -1 (Value not found)
   if(index >= sizeOfArray){return -1;}

   //if array value = key, array index is returned
   if(array[index] == key){return index;}

   //if array value is not equal to key, add to index and call func again                
    return linearSearch(array, index + 1, key, sizeOfArray);                
 }

The only change is that you are passing the starting index into the search function.

Comments

1

When doing this type of search recursively, you must propagate the index you're currently at in your array.

The way you're doing it, there's a global variable, index which is the same in every call to your function because of the static keyword.

The index should rather be a parameter, and each time you call the function recursively, you pass index+1 as a parameter.

Also, you must return the value of the recursive calls, otherwise it just does nothing and doesn't store/process the returned value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.