Solved

# Another Recursive Function Problem

Posted on 2011-02-22
389 Views
"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
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

LVL 37

Expert Comment

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

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

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

LVL 12

Expert Comment

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

Author Closing Comment

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

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

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month2 days, 7 hours left to enroll

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

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