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 :(