473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

A recursion question.

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
ia**********@gmail.com
Oct 21 '06 #1
3 1376
mj
mj wrote:
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
ia**********@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;

}
Oct 21 '06 #2
On Sat, 21 Oct 2006 16:47:07 GMT in comp.lang.c++, mj
<je*************@earthlink.netwrote,
>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?

Oct 21 '06 #3
mj
David Harmon wrote:
On Sat, 21 Oct 2006 16:47:07 GMT in comp.lang.c++, mj
<je*************@earthlink.netwrote,
>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.
Oct 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
4
by: M Hayouka | last post by:
hi can anybody give me a web that I can learn about a recursion function I don't really understand it
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
9
by: JustSomeGuy | last post by:
I have a class that looks something like this (Don't try to compile it, I haven't tested this) class KVP { string key; string value; list<KVP> sublist; };
4
by: Thrillhouse | last post by:
I'm a college computer science major and I'm studying for my final exam by going over previous tests. There's one question that I answered correctly but I cannot, for the life of me, figure out...
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
12
by: Muzammil | last post by:
i want good practice over recursion. can any one give me links for recursion questions site.?? or question.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.