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 5 of 5

Thread: Recursion Trace

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Recursion Trace

    I have this recursion that I created the main for but cant figure out the trace as to why it provides the output it does. Can someone provide me a trace?
    import java.util.*;
     
    public class oddRecurrsion{
     
       public static void main(String[] args){
     
          Scanner stdin = new Scanner(System.in);
     
          System.out.print("Enter an integer: ");
          int input = stdin.nextInt();
     
          System.out.println("Integer \"" + input + 
                             "\" equals " + odd(input));
       } //end main
     
          public static int odd(int number) {
     
                if (number == 1) 
                   return 1;
                else
                   return (2 * number - 1) + odd(number - 1);
          } //end odd
     
    } // end class

    OUTPUT:
    Enter an integer: 5
    Integer "5" equals 25

    Enter an integer: 3
    Integer "3" equals 9
    Last edited by helloworld922; August 18th, 2011 at 10:43 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: Recursion Trace

    What output are you expecting?

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Recursion Trace

    One way to get your own stack traces is to instantiate an Exception in your invoked method, use yourException.fillInStackTrace() (not sure whether that's absolutely necessary or not without reading the API docs) and then yourException.printStackTrace(). Remember not to throw the Exception!

    I think you'd get as meaningful information from:
    System.out.println("in odd, number is " + number);

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion Trace

    I dont believe this is a recursive method. You never actually call on itself. You need a case that ends the recursion and returns a value in addition to the method calling on itself.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Recursion Trace

    Quote Originally Posted by rossums View Post
    I dont believe this is a recursive method.
    Yes it is a recusive method. Do you see else clause?
    return (2 * number - 1) + odd(number - 1); << recursive call right there!
    Improving the world one idiot at a time!

Similar Threads

  1. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  2. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  3. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  4. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM