Solved
Another Recursive Function Problem
Posted on 2011-02-22
"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;
}