Recursive Boolean Function

Write a Recursive Boolean function names isMember. The function should accept two arguments: an array and value. The function should return true if the value is found in the array, or false if the value is not found in the array.

This is the program:

#include<iostream>
using namespace std;

bool isMember(int[], int, int, int);

const int SIZE = 10;

int main()
{
int num[SIZE] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

int numb;

int result;

cout << " Enter the number you would like to search for " << endl;
cin >> numb;

result = isMember(num, 0, SIZE - 1, numb);

if (result == -1)
cout << " The number does not exist in the array " << endl;
else
{
cout << " That number is found in the elemenent " << result;
cout << " in the array " << endl;
}

system("pause");
return 0;

}

bool isMember(int array[], int first, int last, int value)
{
int middle;

if (first > last)
return -1;
middle = (first + last) / 2;
if (array[middle] == value)
return middle;
if (array[middle] < value)
return isMember(array, middle + 1, last, value);
else
return isMember(array, first, middle - 1, value);
}


output:

Enter the number you would like to search for
30
That number is found in the element 1 in the array
Press any key to continue . . .

Enter the number you would like to search for
35
That number is found in the element 1 in the array
Press any key to continue . . .

The output shouldn't be element 1. It should be a different element or numbers like 35 shouldn't be found.

If anyone can find my error I would appreciate it. Thanks Guys
Last edited on
first of all the fact that you are returning signed integers instead of a boolean value is not encouraged at all!

Aceix.
for boolean-int conversion in c++, zero is false and any other nonzero number(incl. negatives) is true. so when you do return -1(in the case of input as 35) in isMember, the function returns true, which is interpreted as 1(in int). I suggest you restructure the programme, but the logic behind the isMember function is considerable.

Aceix.
I totall screwed up the isMember function as the program I put down was the int isMember function program that I did previous. I need help converting it to a bool function and the have the function return true if the value in the array and false if its not
Then youve got fo make these changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isMember(int array[], int first, int last, int value)
{
int middle;

if (first > last)
return false;
middle = (first + last) / 2;
if (array[middle] == value)
return true;
if (array[middle] < value)
return isMember(array, middle + 1, last, value);
else
return isMember(array, first, middle - 1, value);
}


And the way you handle the return value should change.

Aceix.
Last edited on
Now the program is breaking :/

this is what I have:

#include<iostream>
using namespace std;

bool isMember(int[], int, int, int);

const int SIZE = 10;

int main()
{
int num[SIZE] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

int numb;

int result;

cout << " Enter the number you would like to search for " << endl;
cin >> numb;

result = isMember(num, 0, SIZE -1, numb);

if (result == -1)
cout << " The number does not exist in the array " << endl;
else
{
cout << " That number is found in the element " << result + 1;
cout << " in the array " << endl;
}

system("pause");
return 0;

}

bool isMember( int array[], int first, int last, int value)
{
bool middle;

if (first > last)
return false;
middle = (first + last) / 2;
if (array[middle] == value)
return true;
if (array[middle] < value)
return isMember(array, middle + 1, last, value);
else
return isMember(array, first, middle - 1, value);
}
it breaks when I try to put the value 30 or over an for 10 and 20 it said's its found in element 2
middle should remain an integer. From how youve structured it, allowing the programme to display the position of the found number is close to impossible. You can use a reference parameter for that. Also, change the type of result to bool.

Aceix.
The program is not breaking now. But I'm having trouble on how to change it to a reference parameter. It's frustrating how the found number isn't even capable of being found with what I have. I know it might sound much but can you give me an idea or show how it can work with the reference parameter
Topic archived. No new replies allowed.