Connecting Tech Pros Worldwide Help | Site Map

A recursion question.

  #1  
Old October 21st, 2006, 05:35 PM
mj
Guest
 
Posts: n/a
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
iammarkjones@gmail.com
  #2  
Old October 21st, 2006, 06:15 PM
mj
Guest
 
Posts: n/a

re: A recursion question.


mj wrote:
Quote:
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
iammarkjones@gmail.com
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;

}
  #3  
Old October 21st, 2006, 07:15 PM
David Harmon
Guest
 
Posts: n/a

re: A recursion question.


On Sat, 21 Oct 2006 16:47:07 GMT in comp.lang.c++, mj
<jemisonsoftware@earthlink.netwrote,
Quote:
>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?

  #4  
Old October 21st, 2006, 09:05 PM
mj
Guest
 
Posts: n/a

re: A recursion question.


David Harmon wrote:
Quote:
On Sat, 21 Oct 2006 16:47:07 GMT in comp.lang.c++, mj
<jemisonsoftware@earthlink.netwrote,
Quote:
>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?
>
My friend if you are not a teacher you should be. Thank you.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
(academic) math recursion question =?Utf-8?B?bWFyaw==?= answers 8 June 27th, 2008 08:58 PM
Class recursion question cbmeeks answers 5 June 27th, 2008 08:46 PM
Somple Recursion Question Johnny Shih answers 5 November 13th, 2005 09:39 PM
Stack & recursion Question herrcho answers 5 November 13th, 2005 07:49 PM