Q:

Output an array using recurssion

I am trying to print the elements of array through recursion.
int PrintArray(int array[], int size){

    if(size>0){

        --size;
        PrintArray(array,size);

    }

    cout<<array[size]<<" ";

}

int main(){

    int size;

    cout<<"Enter the number of elements to be entered in array ";

    cin>>size;

    int array[size]= {};

    cout<<"The elements to be entered into array are "<<endl;

    for(int i=0; i<size; i++){

        cout<<"element: "<<i<<"- ";

        cin>>array[i];

        cout<<endl;
    }

    PrintArray(array,size);

    return 0;
}

It is printing the first index element two times, how should I fix the issue? 

A:

Think about what happens when size == 1. It is passed in to PrintArray(), then inside the if-statement you set size == 0 and call PrintArray(). After the if-statement you print out array[0]. From the call to PrintArray() with size == 0 it will also print out array[0]. 
To fix this, you should put the cout<<array[size]<<" "; inside the if-statement.

