Hi guys, I need a little help in my assignment. Got it down to the last part but I don't really get recursive functions.
#include <iostream>
#include <ctime>
using namespace std;
void constructArray (int [], int);
void swapArray (int [], int);
int printArray (int [], int);
const int MAX = 20;
int main()
{
srand(time(NULL));
int sizeArray = rand () % MAX;
int makeArray [sizeArray];
constructArray (makeArray, sizeArray);
swapArray (makeArray, sizeArray);
printArray (makeArray, sizeArray-1);
cout << "\n\nSize of Array is " << sizeArray << endl;
system ("pause");
return 0;
}
void constructArray (int makeArray [], int size)
{
for (int i = 0; i < size; i++)
makeArray [i] = rand () % size;
}
void swapArray (int makeArray [], int size)
{
int temp, first;
for (int i = 0; i < size-1; i++)
{
for (int j = 0; j < size-1; j++)
{
if (makeArray [j] % 2 != 0)
{
temp = makeArray [j];
makeArray [j] = makeArray [j+1];
makeArray [j+1] = temp;
}
}
}
}
int printArray (int makeArray [], int size)
{
cout << makeArray [size] << "\t";
if (size == 0)
return makeArray [0];
else
return printArray (makeArray, size-1);
}
See, the problem is that I get an infinite loop if my random number is 0. There's something wrong with the recursive function part. I'm really weak in the recursion part.
Can someone help me with this? Thanks