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;
}
}
#include <iostream>
usingnamespace std;
constint LENGTH = 500;
void arrayInit(int input[], int size);
int recursiveLinearSearch(int input[], int key, int size, bool& methodStatus, int starter);
int main()
{
constint 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;
}
elseif (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.