• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Another Recursive Function Problem

"Cannot convert parameter 2 from int to int*" is the error in the function... can anyone tell me WHY?  I want to call the function again, but with what parameters?

Code:

#include <iostream>

using namespace std;

int binarySearch(int, int*, int);

int main()
{
      int myList[10] = {1,7,13,17,24,38,45,50,100,1000};

      int num;
      int loc;
        int i;

      cout << "The list you entered is: " << endl;
      for (i=0; i < sizeof(myList)/sizeof(int); i++)
         cout << myList << " ";
      cout << endl;

      cout << "Enter search item: ";
      cin >> num;
      cout << endl;

      binarySearch(num, myList, sizeof(myList)/sizeof(int));
      loc = binarySearch(num, myList, sizeof(myList)/sizeof(int));

      if (loc != -1)
            cout << "Item found at position " << loc << endl;
      else
            cout << "Item not in the list" << endl;

      system("pause");
      return 0;
}

int binarySearch(int item, int *list, int length)
{
        int first = 0;
      int last = length - 1;
      int mid;

      if (first <= last)
      {
            mid = (first + last) / 2;

            if (list[mid] == item)
                  return mid;
            else if(list[mid] > item)
                  return binarySearch(item, first, mid - 1);
            else
                  return binarySearch(item, mid + 1, last);
      }
      else
            return -1;
}
0
Member_2_4213139
Asked:
Member_2_4213139
  • 3
  • 2
1 Solution
 
TommySzalapskiCommented:
Okay. When you first call the function you used:
binarySearch(num, myList, sizeof(myList)/sizeof(int));
num is an int, myList is an array (int*), sizeof(myList)/sizeof(int) is an int

This matches the function definition of
int binarySearch(int item, int *list, int length)

The second time you called the function, you used:
return binarySearch(item, first, mid - 1);
item is still an int (good), but first is an int too (the function is expecting an array)
0
 
TommySzalapskiCommented:
Each time you call the function all the variables get reset. The 'list' in the first time you call the function is not visible to the second call. Each one has its own.

You need to set it up like this
int binarySearch(int item, int *list, int start, int length)

Then the first call would look like
binarySearch(num, myList, 0, sizeof(myList)/sizeof(int));

and the other two
                  return binarySearch(item, list, first, mid - 1 + 1);
                  return binarySearch(item, list, mid + 1, last + 1); //need to add 1 since you do last - length -1
Then do
first = start
instead of first - 0
0
 
mwochnickCommented:
your function int binarySearch(int item, int *list, int length) takes the following parameters
int item - an integer
int *list - an integer pointer - which in your case points to an array of integers
int length - an integer

you are trying to call it recursively with three integers you need to call it with a new pointer to the correct point in the list list.  probably the best way is to assign the pointer to a local array variable
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mwochnickCommented:
TommySzalapski's approach is clearer - hope it works for you.
0
 
Member_2_4213139Author Commented:
While it did work, it caused a stack overflow when a number not in the list was chosen... however, this is the best yet!  THANK YOU!!!
0
 
TommySzalapskiCommented:
You could put a condition
If first == last && list[mid] != item
  return -1;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now