0

I understand the array address but this code which I found in a book made me nut.I understand the recursive function too but did not get this one.Here is the code:

int main(){
  const int arraySize = 5;
  int a[arraySize] = { 32, 27, 64, 18, 95};

  cout << "The values in reverse  array are:" << endl;
  someFunction(a, arraySize);
  cout << endl;
  cin.get();
  return 0;
}

void someFunction(int b[], int size)
{
  if (size > 0) {
     someFunction(&b[1], size - 1);
     cout << b[0] << " ";

  }
}

I got this code in a exercise.My question is how it is reversing the array?I will be happy if anyone explain a bit more.thanks

1
  • It isn't reversing anything; but it is sending output in reverse order of the sequence, which ultimately remains untouched (and should be const, btw). Stepping in to that function with a debugger examining the address value at b and the corresponding remaining size would be very telling. Drawing a call-hierarchy with parameter values would be equally so. Commented Mar 17, 2015 at 5:40

2 Answers 2

2

Here is some pseudo-code which shows how the recusive calls to someFunction will be made, and in which order:

someFunction( { 32, 27, 64, 18, 95} , 5)
someFunction( { 27, 64, 18, 95}, 4)
someFunction( { 64, 18, 95}, 3)
someFunction( { 18, 95}, 2)
someFunction( { 95}, 1)
someFunction( { }, 0)

someFunction({ }, 0) will return without doing anything because there is nothing left in the array. Now someFunction will print the first element b[0] of the arrays as it comes out of the recursion, beginning with the array containing only one item {95}:

{ 95 }
{ 18, 95}
{ 64, 18, 95}
{ 27, 64, 18, 95}
{ 32, 27, 64, 18, 95}

So your output will be:

"The values in reverse  array are:"
95 18 64 27 32
Sign up to request clarification or add additional context in comments.

2 Comments

But when it comes out of the recursion how it is going to a loop for array length?why not only 95?
It comes out of the recursion when, instead of calling someFunction() again, it returns. This will happen right after it gets to the bottom of the array. The next line of code in someFunction() after the recursive call to itself is made is to cout the first element of the array. After that, the function returns, allowing the function call higher up in the call chain to also print its value.
1

b[1] is the second element of the array. &b[1] is the address of the second element, so it is just like the original array, but 1 smaller and skipping the first element.

If you print out everything except the first element before you print the first, and do it recursively, the result is it prints out in reverse order.

Note that the actual array has not been reversed. It only gets printed in reverse order. And it's nonsense. A loop is better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.