Solved

Another Recursive Function Problem

Posted on 2011-02-22
6
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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