I was checking all my programs to make sure everything was working properly and all comments were inserted and accurate when...
My recursive function seqSearch didn't work when called a second time within the same driver program. I came back to this thread and researched what was said. I tried my hand at the two options that Mats provided and I can't get either to work.
The code I have now gives me an error at run time. The code I had before ran but the second function call said the number it was searching for wasn't in the list when it was and I think it is because 'loc' isn't getting reset to '0' when it enters the function the second time within the same run.
Mats said:
Perhaps the following is a better implementation, if you insist on using recursion:
Code:
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item, int loc = 0)
{
//Assuming here that length and list are members
if (length == 0 || loc == length)
{
return -1;
}
else if (list[loc] == item)
{
return loc;
}
else
{
return seqSearch(item, loc+1); //THIS IS MY PROBLEM STATEMENT - MENTAL BLOCK!!!
}
}
A slightly neater solution, but requiring adding another member function, would be to have one function seqSearch that calls a helper function that is the actual search, seqSearchImpl(item, loc), avoiding the possibility of some programmer deciding to pass a loc value into the function - this helper function should DEFINITELY be a private function, to avoid misuse of the function.
This code actually works but returns the wrong location in the list.
And this is my current code, but I have eliminated a lot of code - class and member function definitions - that isn't related to this problem:
Code:
template <class elemType>
class arrayListType
{
public:
int seqSearch(const elemType& item);
//Function to search the list for a given item.
//Postcondition: If the item is found, returns the location
// in the array where the item is found;
// otherwise, returns -1.
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
private:
int seqSearchImpl(const elemType& item, int loc = 0);
};
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
int loc = 0;
seqSearchImpl(item, loc);
return loc;
}
template <class elemType>
int arrayListType<elemType>::seqSearchImpl(const elemType& item, int loc)
{
if (length == 0 || loc == length)
return -1;
else if (list[loc] == item)
return loc;
else
{
return seqSearchImpl(item, loc + 1); //recursive call of seqSearch function
}
} //end seqSearch
And this is the part of the driver program that calls the function. I have the same code in the program for two lists.
Code:
//using the recursive seqSearch function
cout << "Enter a number to search for in the list: ";
int num;
cin >> num;
int check = intList.seqSearch(num); //seqSearch Function call
if (check == -1)
cout << "The number you entered was not found in the list." << endl;
else
cout << "The number you entered is at location: " << check + 1
<< " in the list." << endl;
Thank you for your time in helping me.