Recursion

Hello, I have been disturbed by the fact that I can’t make my program do a reading of the recursive search. Any time I enter above or below the limit, the program will always let me know but anytime I enter an exact number inside the parameter, the program is shut down and it can no even do the second if/else statement that is located inside the main function.
From my school of thought, am thinking of the Boolean methodStatus although I can’t be sure about this. Can someone advice accordingly?
Thanks!
Below is m code:.


#include <iostream>
using namespace std;

const int LENGTH = 500;

void arrayInit(int input[], int size);
int recursiveLinearSearch(int input[], int key, int size, bool &methodStatus, int starter);


int main()
{
const int size = LENGTH;
int thatOneArray[size];
arrayInit(thatOneArray, size);
bool methodStatus = NULL;
int key = NULL;
int starter = 0;
char sentinel = 'y';
while (sentinel == 'y' || sentinel == 'Y')
{
cout << "Please enter the value you would like to search for: ";
cin >> key;
if (key > 0 && key < size)
{
break;
}
else
{
cout << "Please enter a value greater than 0 and less than " << size << "." << endl;
}

int index = recursiveLinearSearch(thatOneArray, key, size, methodStatus, starter);

if (methodStatus == true)
{
cout << "The value was in position number " << index + 1 << "." << endl;
cout << "The function was called " << index << " times." << endl;
}
else if(methodStatus == false)
{
cout << "The number does not exist in the array" << endl;
cout << "The function was called " << index << " times" << endl;
}
cout << "Would you like to continue? enter y or Y, all other entries exit program: ";
cin >> sentinel;
}

return 0;
}


void arrayInit(int input[], int size)
{
for (int i = 0; i < size; i++)
{
input[i];

}
cout << "array initialized" << endl;
}



int recursiveLinearSearch(int input[], int key, int size, bool& methodStatus, int starter)
{
if (input[starter] == 0)
{
methodStatus = true;
return starter;
if (starter > size && starter != key)
{
methodStatus = false;
return 0;
}
else
{
int returner = recursiveLinearSearch(input, key, size, methodStatus, starter);
return returner;
}
}

}
.
Please use code tags when posting code so that the code is readable!


[code]
// code goes here
[/code]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

const int LENGTH = 500;

void arrayInit(int input[], int size);
int recursiveLinearSearch(int input[], int key, int size, bool& methodStatus, int starter);


int main()
{
	const int size = LENGTH;
	int thatOneArray[size];
	arrayInit(thatOneArray, size);
	bool methodStatus = NULL;
	int key = NULL;
	int starter = 0;
	char sentinel = 'y';
	while (sentinel == 'y' || sentinel == 'Y')
	{
		cout << "Please enter the value you would like to search for: ";
		cin >> key;
		if (key > 0 && key < size)
		{
			break;
		}
		else
		{
			cout << "Please enter a value greater than 0 and less than " << size << "." << endl;
		}

		int index = recursiveLinearSearch(thatOneArray, key, size, methodStatus, starter);

		if (methodStatus == true)
		{
			cout << "The value was in position number " << index + 1 << "." << endl;
			cout << "The function was called " << index << " times." << endl;
		}
		else if (methodStatus == false)
		{
			cout << "The number does not exist in the array" << endl;
			cout << "The function was called " << index << " times" << endl;
		}
		cout << "Would you like to continue? enter y or Y, all other entries exit program: ";
		cin >> sentinel;
	}

	return 0;
}


void arrayInit(int input[], int size)
{
	for (int i = 0; i < size; i++)
	{
		input[i];

	}
	cout << "array initialized" << endl;
}



int recursiveLinearSearch(int input[], int key, int size, bool& methodStatus, int starter)
{
	if (input[starter] == 0)
	{
		methodStatus = true;
		return starter;
		if (starter > size && starter != key)
		{
			methodStatus = false;
			return 0;
		}
		else
		{
			int returner = recursiveLinearSearch(input, key, size, methodStatus, starter);
			return returner;
		}
	}

}


What is line 56 supposed to do?

L15/16 - you shouldn't initialise a bool or int with NULL. Use true/false for bool and an integer number for int.

recursiveLinearSearch() If input[starter] != 0, then the function ends with no value returned - which is incorrect as the function is to return a value of type int.
Topic archived. No new replies allowed.