Recursive Array Functions help?

I have most of my code written (3 recursive functions, one to fill an array with 50 randomly generated numbers between 1 and 500, one to print out all 50 numbers, and a binary search function to show the position of any chosen number. I had it working until I changed the FillRand function and Print function to recursive instead of loops, and the program does not do anything when ran anymore. It's really something simple I'm sure, I just can't find it.


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void FillRand(int randArray[], int); // random number function
void Print(int randArray[], int); //function to print out array
int Search(int randArray[], int, int, int); //search for a #


int main()
{
srand(time(NULL)); //sets random seed for generating random numbers


int I_SIZE = 50;
int randArray[I_SIZE]; //set MAX size of array to 50.
int numtofind;
int result;
int x = 0;
int y = 0;
int first = 0;
int last = 49;

FillRand(randArray, x);

Print(randArray, y);

cout << "\nPlease enter in a number to find it's position or -99 to quit: " << endl;
cin >> numtofind;


result = Search(randArray, first, last, numtofind);
if ( result == -1 )
{
cout << "Number not found" <<endl;
}
else
{
cout <<"Number is found in position " << result << endl;
}

return 0;
}

void FillRand(int randArray[], int x)
{

while(x < 50)
{ randArray[x] = (rand() % 500 + 1);
FillRand(randArray, x+1);
}
}

void Print(int randArray[], int y)
{
if(y < 50)
{cout << randArray[y] << " ";
Print(randArray, y+1);
}
}

int Search(int randArray[], int first, int last, int numtofind)
{
int middle;
while(numtofind != -99)
if(first > last)
return -1;
middle = (first + last) / 2;
if(randArray[middle] == numtofind)
return middle;
if(randArray[middle] < numtofind)
return Search(randArray, middle + 1, last, numtofind);
else
return Search(randArray, first, middle - 1, numtofind);


}
Last edited on
Topic archived. No new replies allowed.