Solved

Another Recursive Function Problem

Posted on 2011-02-22
6
391 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

623 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