Understanding a recursive solution to print out an array in reversed order

一笑奈何 提交于 2020-01-05 11:47:31

问题


I'm still new to C++ and as of right now learning recursion. I wanted to display the elements in reversed order without starting the index of the array at the end using recursion.

Obviously it would be very easy to do using loops, but using recursion is a different matter. I found this solution to the problem online, but can't understand how exactly its able to print out each value.

void recArrayBackPrint(int array[],int size)
{
   if (size > 0)
   {
      recArrayBackPrint(array+1,size-1);
      cout << array[0] << "   ";
   }
//  base case is empty array (size == 0), so do nothing
}

I understand that in this case, array + 1 would be referring to the memory address located at the current element + 1. After trying to trace it using something like:

int main() {
    int values[5] = {1,2,3,4,5}
    recArrayBackPrint(values,5);
}

I can get all the way down to the size being 0, but still have no idea how it would be able to print out array[4], array[3].. etc. In my mind it should be hitting recArrayBackPrint and going all the way to size 0, then doing nothing at all.

So what exactly is going on here?


回答1:


first time in recArrayBackPrint,it's printing the first element of {1,2,3,4,5},then 1.
but before the output, it enters the second recArrayBackPrint to print the first element of {2,3,4,5} ,then 2.
still, before output 2, the third recArrayBackPrint to print 3 in {3,4,5}.
and all the way to the last recArrayBackPrint to print 5 in {5}. then the output order is:
5th recArrayBackPrint (5)
4th recArrayBackPrint (4)
3rd recArrayBackPrint (3)
2nd recArrayBackPrint (2)
1st recArrayBackPrint (1)

I hope I make myself clear.




回答2:


Until the size is reduced to zero, all the function does is call itself recursively without printing anything, so the stack ends up with parameters (array+1, size-1), (array+2, size-2) and return addresses. Once the size is zero, then the function returns and prints the last character, (the one at array[size-1]), then that instance returns and prints the next to last character, and so on until until it gets to the first instance of the function called where it prints the first character.




回答3:


Assume recArrayBackPrintTMP was a function that also printed an array. When it returns after printing the remaining array your recArrayBackPrint would print the first element, instead of doing nothing at all.

void recArrayBackPrint(int array[],int size)
{
   if (size > 0)
   {
      recArrayBackPrintTMP(array+1,size-1);
      cout << array[0] << "   ";
   }
//  base case is empty array (size == 0), so do nothing
}

Assume next that recArrayBackPrintTMP was similar but in turn called recArrayBackPrintTMP2 which printed all elements 2 onwards in reverse.

void recArrayBackPrintTMP(int array[],int size)
{
   if (size > 0)
   {
      recArrayBackPrintTMP2(array+1,size-1);
      cout << array[0] << "   ";
   }
//  base case is empty array (size == 0), so do nothing
}

Now assume that instead of writing all those similar functions we are just re-using the same function when doing recursion.




回答4:


So the psuedocode is approximately this:

To print out an array of N values backwards:
    Print out the last (N-1) backwards
    Print out the first one.

So:

To print out the numbers from 1-5 backwards:
  print 2-5 backwards; to do this:
     print 3-5 backwards; to do this:
       print 4-5 backwards; to do this:
         print 5 backwards; to do this:
           print nothing backwards; to do this:
             do nothing
         print 5
       print 4
      print 3
    print 2
  print 1


来源:https://stackoverflow.com/questions/25656821/understanding-a-recursive-solution-to-print-out-an-array-in-reversed-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!