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 83 84
|
#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;
}
}
}
|