Results 1 to 6 of 6

Thread: recursive function, might have inf loop

  1. #1

    Thread Starter
    New Member Sevengraff's Avatar
    Join Date
    Feb 2003
    Location
    California, USA
    Posts
    10

    Question recursive function, might have inf loop

    I have to use a recursive function to print a ruler to the screen. it looks knida like this:
    Code:
    |       |       |
    |   |   |   |   |
    | | | | | | | | |
    |||||||||||||||||
    execept it will be about 65 characters long, and 7 lines deep.

    here is my code:

    PHP Code:
    // ruler.cpp
    // ---------------------
    // uses a recursive function to print a ruler to the screen

    #include <iostream.h>

    void subdivideint loint hiint levelchar line[])
    {
        if(
    level == '0')
            return;
        
    // get and mark midpoint
        
    int mid = (lo hi) / 2;
        
    line[mid] = '|';
        
    // call again for left side
        
    subdivide(lomid-1level-1line);
        
    // call for right side
        
    subdivide(mid+1hilevel-1line);
        return;
        
    }

    int mainvoid )
    {
        
    char line[66];
        
    int i;

        
    // prepair the array
        
    for(i=0i<66i++)
        {
            
    line[i] = ' ';
        }
        
    line[0] = '|';
        
    line[64] = '|';
        
    cout << line << "\n\n\n";
        for(
    i=0i<7i++)
        {
            
    subdivide(065iline);
            
    cout << line << endl;
        }

        return 
    0;

    I dont get any errors when I compile it in VC++ 6, but when i run the exe it dies saying there is an error. when i try to use the debug thing, it says stack overflow. It all makes sense in my head, so i assume i have an infinate loop somewhere that i cant see because im a new programmer.
    Can anyone help?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    There's an iterative solution to this as well,
    Code:
    for (int level=8;level>0;level/=2){
      for (int x=0;x<66;x++)
        cout << x%level?' ':'|';
      cout << endl;
    }
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Recursion is one of those things teachers think is cool, but is actually not even remotely useful to real application development, except in specially controlled conditions.

    Here's why:
    Every time a function calls itself:
    It pushes all of the contents of the registers onto the stack, then
    it pushes the contents of all of the local variables onto the stack.
    Then it copies the parameters the function needs, and puts them on the stack, too. The function pops (reads) the stack -just the parameters only- when it is called. All of the other stuff remains on the stacks, and fills up the stack rapidly.

    For this reason, recursion is very memory intensive, and tends to be extremely slow compared to an iterative solution (like Keds gave you).

    Another major bad side to recursion is that reading your highly recursive code six months later is not easy - translation: it is hard to maintain. Even when you were the autho.

    Lastly, you found out why recursion ii general is not a viable solution -stack overflow.

    The only practical uses for recursion that I know about are writing descent parsers and some quicksort algorithms. And I've been writing code for a VERY long time.

    This obviously is class work because, unless you're entering a contest, professional programmers usually don't do stuff like that. If you do, they stop paying you - if you get my drift. Teachers do not seem to know this fact...

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I found recursion useful in hierarchical class object hierarchies (where the this pointer is the changing recursion parameter) useful too. That's also the only case I can think of where recursion is easier to understand than iteration.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Or similar situations. Basically whenever I have to visit every node of a tree I use iteration to visit siblings and recursion to walk through depth levels.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Recursion is the way in declarative languages, since its much more wider concept than plain iteration. Recursion in imperative languages are just lowlevel and bound to hardware which is why programmers avoid it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width