# Help!!!(Anyone that truly understands recursion)

This was a question from my lab at class.  It's over, but I still don't quite understand it.  They say if I can understand this question I know recursion.  can someone please explain how this works.

Provided below is a function.  Simply stick it in main and have an int passed into it and the fun begins.

void f(int n) {
if (n > 1) {
cout << 'a';
f(n/2);
cout << 'b';
f(n/2);
}
cout << 'c';
}

The function output of the code is easy when the numbers passed into is 1,2, or 3.  However, I get losts when it starts going to 4.  If anyone can help me please tell me the output of this code when a 4 is passed into the funciton.  If you can please tell me how you got it.  If possible explain with values of 5,6,7 as well.  Thank You.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
1       c
2       acbcc
3       acbcc
4       aacbccbacbccc
5       aacbccbacbccc
6       aacbccbacbccc
7       aacbccbacbccc
8       aaacbccbacbcccbaacbccbacbcccc
0
Commented:
Um this program is recursive in the sense that it calls itself with the value n/2.

I'll try to simplify the way you can get the value of f(n) for a certain value of n:

-------------------- THEORY ---------------------

So for another value than '1':

The function :
- 1 - prints 'a'
- 2 - then calls itself but with the value n/2
- 3 - prints 'b'
- 4 - then calls itself but with the value n/2
- 5 - prints 'c'

Basically the output is :
'a'
Output for the value n/2
'b'
Output for the value n/2
then 'c'

-------------------- EXAMPLES ---------------------

So if we call Output (n) the output for the value n, for the value n=2, we have:

Output (n=2) = 'a' + Output(1) + 'b' + Output(1) + 'c'
Output (n=2) = 'a' + 'c' + 'b' + 'c' + 'c'
Output (n=2) = 'acbcc'

Output (n=3) = 'a' + Output(1) + 'b' + Output(1) + 'c'
Output (n=3) = 'a' + 'c' + 'b' + 'c' + 'c'
Output (n=3) = 'acbcc'

Output (n=4) = 'a' + Output(2) + 'b' + Output(2) + 'c'
Output (n=4) = 'a' + 'acbcc' + 'b' + 'acbcc' + 'c'
Output (n=4) = 'aacbccbacbccc'

i m sure you can figure the values for greater values yourself using this method ^_^
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thank you BlueTrin for your help.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.