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

line 16: I don't know what compiler you are using but mine won't compile that line because arrays expects a const value. Maybe there is something new in C99 ? Most compilers will not like that line.

>>I get an infinite loop if my random number is 0
Yes because you can't have arrays of size 0 int array[0] is not a valid array. Maybe add 1 to the rand % MAX value, then change MAX to 19 instead of 20 to avoid that problem.

Aaaaa ok, I didn't notice that about the code. I got kind of mixed up with finding out why 0 don't work.

Line 16's declaring an array with a size randomly produced.
I'm using Dev C++ as my c++ compiler.

Thanks for pointing me the right direction.
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.