Solved

Another Recursive Function Problem

Posted on 2011-02-22
6
382 Views
Last Modified: 2012-05-11
"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
Comment
Question by:Member_2_4213139
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34956814
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
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 34956834
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
 
LVL 12

Expert Comment

by:mwochnick
ID: 34956864
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:mwochnick
ID: 34956879
TommySzalapski's approach is clearer - hope it works for you.
0
 

Author Closing Comment

by:Member_2_4213139
ID: 34956979
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34957068
You could put a condition
If first == last && list[mid] != item
  return -1;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now