Recursive Function in Pseudo Code

This is a discussion on Recursive Function in Pseudo Code within the Tech Board forums, part of the Community Boards category; Hi. Can someone help me analyze this code. I think it runs into an infinite loop at n=3. Code: Fr(n): ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Recursive Function in Pseudo Code

    Hi. Can someone help me analyze this code. I think it runs into an infinite loop at n=3.
    Code:
     Fr(n):
     if n=1
       print n;
     else
       for i=1 to n-1 do
         Fr(i)
    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    if ypou are going to play n-1 as an ending parameter you must decrease n somewhere,

  3. #3
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Seems to Work

    Look at this. It seems to run as I was told it should. For example when n=6 the function outputs "1" 16 times. Why?!
    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    void Fr(int);
    
    int main()
    {
      int n;
      n=0;
      cout<<"Please enter a value for n: "<<endl;
      cin>>n;
      Fr(n);
      system("pause");
      return 0;
    }
    
    void Fr(int m)
    {
      int i;
      if(m==1)
      {
        cout<<m<<endl;
      }
      else
      {
        for(i=1; i<=m-1; i++)
        Fr(i);
      }
    }
    I can't understand it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I can't understand it.
    That's no surprise. You're mixing recursion with iteration and loop indices with the original argument that was not a loop index to begin with.

    Start at the top with, say, m = 3. Going from the outside in, we realize that the outer most call will loop twice because i starts at 1 and goes to m - 1. Then we break away from m and call Fr with the loop index 1 that starts from 1 every time.

    The first iteration of the loop is 1, so m is printed in the first recursive call. In the second recursive call, m is 2 so m is not printed and the first recursive loop begins. Because m is 2, the loop only executes once with the argument of 1, thus printing the value of m.

    After this all recursive paths return and the output is:
    Code:
    1
    1
    Use the same logical progression for larger values of m and you'll have a good understanding of how this function works.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21