A recursion question.

M

mj

I am a newbie to C++ but not to programming. I am proficient in VB 6.0
and have some skills in VB.net. Recursion is a new idea for me and being
from the VB world I rested heavily on stepping through code to learn the
order code would execute in. I am using Dev-C++ with the MinGW compiler.
I understand the syntax:
void counter(int End)
{
cout<< " ";
cout<< End;
if( End != 0 ){
counter(End-1);
}
}
My brain seems to get tangled with trying to visualize the way the code
coils and uncoils. I have stepped through this twenty or thirty times
but I suppose I am looking for some caveat to help gel the concept.
Something like how would this look sitting on the stack?
Thank you all for sharing your experience with the community.
Mark Jones
(e-mail address removed)
 
M

mj

mj said:
I am a newbie to C++ but not to programming. I am proficient in VB 6.0
and have some skills in VB.net. Recursion is a new idea for me and being
from the VB world I rested heavily on stepping through code to learn the
order code would execute in. I am using Dev-C++ with the MinGW compiler.
I understand the syntax:
void counter(int End)
{
cout<< " ";
cout<< End;
if( End != 0 ){
counter(End-1);
}
}
My brain seems to get tangled with trying to visualize the way the code
coils and uncoils. I have stepped through this twenty or thirty times
but I suppose I am looking for some caveat to help gel the concept.
Something like how would this look sitting on the stack?
Thank you all for sharing your experience with the community.
Mark Jones
(e-mail address removed)
OK.. I answered my own question with this.

#include <iostream>
using namespace std;
double Factorial(int fact);
int main(int argc, char *argv[])
{
for(int a = 1; a < 6; a++){
cout<< Factorial(a);
cout<< "\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}

double Factorial(int fact)
{

if(fact <= 0){
return 01;}
else{
fact = fact * Factorial(fact-1);
return fact;
}
return fact;

}
 
D

David Harmon

On Sat, 21 Oct 2006 16:47:07 GMT in comp.lang.c++, mj
I understand the syntax:
void counter(int End)
{
cout<< " ";
cout<< End;
if( End != 0 ){
counter(End-1);
}
}
My brain seems to get tangled with trying to visualize the way the code
coils and uncoils. I have stepped through this twenty or thirty times
but I suppose I am looking for some caveat to help gel the concept.

How to gel the concept? Well, I don't know any simple answer except
to keep trying. Looking at it from different viewpoints helps, so
here's one:

Imagine a sheet of paper. On it is drawn a rectangle representing
an activation of counter(). I'm going to start by calling
counter(3), so the rectangle contains shorthand like
counter(3) {
cout << 3;
counter(2);
}

Now when you get to the counter(2) call above, take an imaginary
Post-it (trademark 3M) note and stick it down over the rectangle.
On it is written:
counter(2) {
cout << 2;
counter(1);
}

Naturally, there will be another Post-it to go on top of that, on
which says:
counter(1) {
cout << 1;
counter(0);
}

The final Post-it says:
counter(0) {
cout << 0;
// done
}

Since "if( End != 0 )" was not satisfied this time, there is no
additional Post-it on top of that. Instead you get to the }, peel
off the Post-it, and you are back at the previous one. But there is
nothing left to do there either, so peel it off too. And the next,
and the next, until you are all the way back to the original.

Any better?
 
M

mj

David said:
On Sat, 21 Oct 2006 16:47:07 GMT in comp.lang.c++, mj


How to gel the concept? Well, I don't know any simple answer except
to keep trying. Looking at it from different viewpoints helps, so
here's one:

Imagine a sheet of paper. On it is drawn a rectangle representing
an activation of counter(). I'm going to start by calling
counter(3), so the rectangle contains shorthand like
counter(3) {
cout << 3;
counter(2);
}

Now when you get to the counter(2) call above, take an imaginary
Post-it (trademark 3M) note and stick it down over the rectangle.
On it is written:
counter(2) {
cout << 2;
counter(1);
}

Naturally, there will be another Post-it to go on top of that, on
which says:
counter(1) {
cout << 1;
counter(0);
}

The final Post-it says:
counter(0) {
cout << 0;
// done
}

Since "if( End != 0 )" was not satisfied this time, there is no
additional Post-it on top of that. Instead you get to the }, peel
off the Post-it, and you are back at the previous one. But there is
nothing left to do there either, so peel it off too. And the next,
and the next, until you are all the way back to the original.

Any better?
My friend if you are not a teacher you should be. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top