I have a question related to a recursive function i have


I have a question related to a recursive function. I have ran it and got it to do what it is supposed to. However, I don't fully understand how it is actually working and how it gets the answer

here is the function:
void G(char ch, int n)
{
if (n<=0)
cout<< endl;
else
{
G(ch-1, n-1);
cout<< ch;
G(ch-1,n-1);
}
}

in the main when you pass in G('M', 2) the print out is this;
L
M
L

when you pass in G('M',3) the print out is:
K
L
K
M
K
L
K

It seems to me it should read in the ch and n and then if n is not <= 0 it skips to G(ch-1, n-1) which seems like it would take M and subtract 1 and then n(2) and subtract one. If someone could take me through the recursive function step by step to show me how it gets the answer, that would be great.

Solution Preview :

Prepared by a verified Expert
Programming Languages: I have a question related to a recursive function i have
Reference No:- TGS01245868

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)