?
Solved

Recursive Function Problem

Posted on 2011-02-22
5
Medium Priority
?
369 Views
Last Modified: 2012-06-27
The function binarySearch used to be iterative, and now I'm trying to make it recursive.  However, for some reason, I get an error when I try to pass the 2nd parameter... can anyone give me a hint as to what I'm doing wrong?

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
5 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34956451
Try it like this
int binarySearch(int item, int *list, int last, int start = 0)
For the first call, send length -1 as last and leave start blank.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1500 total points
ID: 34956462
Your problem is that you didn't change the function definition but tried to send it more items. You were sending an int where it was expecting a int* (array of ints)
0
 

Author Comment

by:Member_2_4213139
ID: 34956515
OK, I changed the function to:

int binarySearch(int item, int *list, int last, int start = 0)

but it didn't like that...  so I put it back to:

int binarySearch(int item, int *list, int length, int start = 0)

and:

"none of the 2 overloads could convert all the argument types"

As for sending it an int when it was expecting an int*, where does that occur?  I mean, I would send it an int* if I could!!!
0
 

Author Closing Comment

by:Member_2_4213139
ID: 34956718
This didn't solve the entire problem, but it needed to be corrected...
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34956762
int* is an array. You had it set up to take an array as the second argument but in some places you were sending first or last there. You just needed to set up the function to take the right things in the right order. What else do you need? Why did you give a B? I will still help until everything is fixed.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

719 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