Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,543 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,541 people online right now. Registration is fast and FREE... Join Now!




Recursion

 
Reply to this topicStart new topic

Recursion, Multiple Recursive Calls

Pourang
25 Nov, 2007 - 10:29 AM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 9


My Contributions
Hi.

I'm reading C++ Primer Plus Fifth Edition by Stephen Prata. Chapter 7, page 326 : Ruler.cpp

example tries to explain how to write a interesting program. You see the code below and guess what? I see it too but it's just to complicated and the books explanation is not making that clearer. If i can't get it there is no point to continue to study c++. so i hope you can give me a hint sad.gif(:confused:



CODE

#include <iostream>
using namespace std;

const int Len = 66;
const int Divs = 6;


void subdivide(char ar[], int low, int high, int level);
int main()
{
    char ruler[Len];
    int i;
    for (i = 1; i < Len - 2; i++)
        ruler[i] = ' ';
    ruler[Len - 1] = '\0';
    int max = Len - 2;
    int min = 0;
    ruler[min] = ruler[max] = '|';
    std::cout << ruler << std::endl;
    for (i = 1; i <= Divs; i++)
    {
        subdivide(ruler,min,max, i);
        std::cout << ruler << std::endl;
        for (int j = 1; j < Len - 2; j++)
            ruler[j] = ' ';  // reset to blank ruler
    }
    

    cin.get();
    cin.get();

    return 0;
}

void subdivide(char ar[], int low, int high, int level)
{
    if (level == 0)
        return;
    int mid = (high + low) / 2;
    ar[mid] = '|';
    subdivide(ar, low, mid, level - 1);
    subdivide(ar, mid, high, level - 1);
}






User is offlineProfile CardPM
+Quote Post

Bench
RE: Recursion
25 Nov, 2007 - 12:33 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
What exactly don't you understand? have you compiled and run the program to see its output? Recursion is a form of repetition whereby a function invokes a call to itself, causing multiple nested calls of the same function to be present on the stack at once

If this is your first outing with recursion, then you might want to try a simpler example, such as one which outputs Fibonacci's sequence.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Recursion
25 Nov, 2007 - 12:51 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
QUOTE
If i can't get it there is no point to continue to study c++


Not true. Understanding other people's code is not easy. It can be hard to wrap your head about another person's logic. I think anyone who has spent any time answering post here can attest to it.

lets begin by looking at what the program does. It prints out a text-"ruler":

|...............................................................|
|...............................|...............................|
|...............|...............|...............|...............|
|.......|.......|.......|.......|.......|.......|.......|.......|
|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


It does so by inserting bars '|' into a string in the right places. Then it calls subdivide() with the level set to 1, and low and high set to the outer two bars. This causes subdivide() to insert a bar in the middle (high + low)/2 = the average = the middle. Then subdivide tries to call itself with level = 0 and low and high set to match the lower region and the upper region on the string... but since level = 0 these calls just return without doing anything. The next time it is called it has level = 2, so this time when it recursively calls itself it will add in a bar to the midpoint of the upper and lower regions. It does this by first setting low = min, high = mid (this works for the lower half), and then low = mid, high = max (this works for the upper half).

This process is called bisection and it is an important concepts in computer science. Take for instance a binary search. Lets say I want to guess what number you are thinking of between 1 and 100. When I give a guess you tell me if I am high, low, or right on.

I start by guessing in the middle of my range: 50
if you say high, then I take the middle of the lower half: 25
if you say low, then I guess the middle of the upper half: 75.
The process continues this way... me always guessing the mid point (low number + high number)/2
this quickly brackets the number.

here is a comment version of the code:
CODE
#include <iostream>
using namespace std;

const int Len = 66;
const int Divs = 6;


void subdivide(char ar[], int low, int high, int level);
int main()
{
    char ruler[Len];
    int i;
    
    //---  Initialize the string to a blank  --
    for (i = 1; i < Len - 2; i++)
        ruler[i] = ' ';
    ruler[Len - 1] = '\0'; //the end of strings in c are all marked with a zero character

    //--- Set up our meta-data
    int max = Len - 2;
    int min = 0;

    //Add the two ending bars for the inital ruler...
    ruler[min] = ruler[max] = '|';
    //** at this point the ruler is initalized. One bar on either end.
    //     this never changes, only the inner characters are changed...
    
    std::cout << ruler << std::endl;

    for (i = 1; i <= Divs; i++)
    {
        subdivide(ruler,min,max, i);
        std::cout << ruler << std::endl;
        for (int j = 1; j < Len - 2; j++)
            ruler[j] = ' ';  // reset to blank ruler
    }
    

    cin.get();
    cin.get();

    return 0;
}

//This function will add a bar to the middle of the range min to max
//   It will then pass the lower half to another copy of itself
//   and the upper half to another copy of itself...
//   The argument 'level' determines how many times this happens.
// if level == 0 it does nothing.
// if level == 1 it adds 1 bar
// if level == 2 it adds 3 bars... on in the middle, one in the upper side, on in the lower side...
void subdivide(char ar[], int low, int high, int level)
{
    if (level == 0)
        return;
    int mid = (high + low) / 2;
    ar[mid] = '|';
    subdivide(ar, low, mid, level - 1);
    subdivide(ar, mid, high, level - 1);
}

User is online!Profile CardPM
+Quote Post

Pourang
RE: Recursion
25 Nov, 2007 - 02:48 PM
Post #4

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 9


My Contributions
QUOTE(Bench @ 25 Nov, 2007 - 01:33 PM) *

What exactly don't you understand? have you compiled and run the program to see its output? Recursion is a form of repetition whereby a function invokes a call to itself, causing multiple nested calls of the same function to be present on the stack at once

If this is your first outing with recursion, then you might want to try a simpler example, such as one which outputs Fibonacci's sequence.


I just can't see the whole code heng together but right now i try to isolate some part and figure out what it does. Before i ask a better question here i try to study a little more. Thanks for your time.
User is offlineProfile CardPM
+Quote Post

Pourang
RE: Recursion
25 Nov, 2007 - 02:54 PM
Post #5

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 9


My Contributions
QUOTE(NickDMax @ 25 Nov, 2007 - 01:51 PM) *

QUOTE
If i can't get it there is no point to continue to study c++


Not true. Understanding other people's code is not easy. It can be hard to wrap your head about another person's logic. I think anyone who has spent any time answering post here can attest to it.


Thanks for the explanation. Tomorrow I'll l try to read the code again. My reading is like my eating: "quick", becouse i like to see more of c++ future. So i try to come done now :-)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month