Hey
i am beginner and i read a book about c++,and i like to do all the exercises alone until i find alone the solution.
but one of the topics that give me a hard time is recursions.
i am on an exercise with the following code.

#include <iostream>
using namespace std;

void somefuction(int[],int,int);

int main()
{
    const int arraysize=10;
    int a[arraysize]={1,2,3,4,5,6,7,8,9,10};

    cout<<"The values in the array are: "<<endl;
    somefuction(a,0,arraysize);
    cout<<endl;
}

void somefuction(int b[],int current,int size)
{
    if (current<size)
    {
        somefuction(b,current+1,size);
        cout<<b[current]<<"  ";
    }
}

the question is what that code do,before i compile it to see the solution i read it and i figured out that prints the array values like, 1,2,3,4,5,6,7,8,9,10 but after i compiled it,by suprice was in decreasing order 10,9,8,7,6,5,4,3,2,1.

i want if anyone can give me a hint how the recursive somefuction() works.

when main calls somefuction() current is 0 and size 10,i understand that call it self until if statement be valid and prints 10,but how goes after to 9 and decrease?

as i see it somehow "current" must replace "size" so the "if" statement would be every time one loop less but i cant see it :(

Think of the process going on a stack. When your code enters somefunction, the if statement is checked and when true, it immediately calls somefunction again (not executing the cout statement yet.) All of these function calls are placed on a stack until the somefunction actually returns so you end up with (example shorted to 3 items in the array)

somefunction(a,0,3){
  if (0<3){
    somefunction(a,1,3){
      if (1<3){
        somefunction(a,2,3){
          if (2<3){
            somefunction(a,3,3){
              if (3<3){
              }
              (returns because if was false)
            }
            cout<<a[2]<<" "
          }
        }
        cout<<a[1]<<" "
      }
    }
    cout<<a[0]<<" "
  }
}

Then put those statements that output starting with the top one:
            cout<<a[2]<<" "
        cout<<a[1]<<" "
    cout<<a[0]<<" "

3 2 1

In your if statement on line 18, you check to see whether current is less than size, which it is. It then calls somefunction again to increment current by 1. However, 'b' isn't able to be outputted yet because when you call 'somefunction' within your if loop, it brings it back out of the loop so that it can again check whether the expression is true or not. The loop will keep going until current is = to size. Once the statement is true, THEN it will skip over the call to 'somefunction' and output the values of b[current] which now starts at 10.


Edit: woops, looks like djarn beat me to it... :)

thanks both for your replies understood now,but still i dont like recursive fuctions :P

>>the question is what that code do,before i compile it to see the solution i read it and i figured out that prints the array values like, 1,2,3,4,5,6,7,8,9,10 but after i compiled it,by suprice was in decreasing order 10,9,8,7,6,5,4,3,2,1.

it was printed in that order because when (current > size)
it exit the function and do nothing , after that the control goes back to the previous function call where( size = current) and do the statement after the function call exactly which prints the a[current]and so on

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.