Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Convert this to non-recursive

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Cool Convert this to non-recursive

    public static int f(int a, int b)
        {
            Z.h1(a, b);           //// arbitrary code
     
            if (Z.h2(a, b))
                return Z.h3(a, b);
     
            else
            {
                Z.h4(a, b);        ////arbitrary code
                return f(Z.h5(a, b), Z.h6(a, b));      //// recursive call
            }
        }

    -h1 and h4 --------------------> void functions
    -h2 ------------------------------> boolean function
    -h3, h5 and h6 -------------->return int
    -h1 ... h6-----------------------> will not call f

    My Attempt to convert the code above to non-recursive:

     
    public static int f(int a, int b)
    {
        while(true)
        {
            Z.h1(a, b); //// arbitrary code
     
            if (Z.h2(a, b)) //// base case
            return Z.h3(a, b);
            else
            {
                Z.h4(a, b); /////arbitrary code
     
                int temp = Z.h5(a, b);
                b = Z.h6(a, b));
                a=temp;
             }
        }
    }

    I'm not sure if I'm right.
    Any ideas?
    Last edited by IAmHere; June 9th, 2011 at 09:55 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convert this to non-recursive

    It looks fine to me. Did you test it out to make sure the outputs match up?

Similar Threads

  1. Recursive multiplication and Karatsuba
    By jsp01 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2011, 03:04 AM
  2. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 04:05 PM
  3. recursive descent parser
    By gammaman in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 21st, 2010, 04:50 AM
  4. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 09:36 PM
  5. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 02:46 PM