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

Thread: Problem with recursive method. Can you help?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Problem with recursive method. Can you help?

    The point of my code is to find the answer of logbn. I am stuck on the part where a is incremented. I'm not exactly sure where to increment it because every value input will return 1. Any advice? Thanks!
    So here is my code.


    /**
    * Calculates base-b log of n recursively
    * @param b the base of the logarithm to be found
    * @param n the value whose logarithm is being found
    * @return base b log of n
    * Pre-Condition: n must be integer multiple of b
    * @since Lesson 6-4
    * @version 12-6-11
    * @author TFLeGacY
    */
    public int log(int b, int n)
    { int a = 0;
    if(n / b == 1)
    return 1;
    else
    { log(b, n = n / b);
    a++;
    }
    return a;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Problem with recursive method. Can you help?

    One of your problems could be due to integer division. There are no fractions. 10/6 = 1

  3. The Following User Says Thank You to Norm For This Useful Post:

    TFLeGacY (December 6th, 2011)

  4. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Problem with recursive method. Can you help?

    Another problem could be the lack of storing return values
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    TFLeGacY (December 6th, 2011)

  6. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,618
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Problem with recursive method. Can you help?

    The OP needs to post the algorithm that the code is supposed to implement.

  7. The Following User Says Thank You to Norm For This Useful Post:

    TFLeGacY (December 6th, 2011)

  8. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with recursive method. Can you help?

    Thanks so much for the help you guys! It got it working great! In case you were curious, here is the code that calculates the base-b log of n.

    public int log(int b, int n)
    { if(n / b == 1)
    return 1;
    else
    { return 1 + log(b, n = n / b);
    }
    }

  9. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Problem with recursive method. Can you help?

    You should be aware that your solution whilst working has a couple of redundant features, I'd recommend having a think about what they might be if you want to excel in programming and problem solving

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  10. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with recursive method. Can you help?

    @Freaky Chris, the last post I put up was the exact answer for my homework. I do plan on excelling in programming and I just wanted to let you know that this code was simply an assignment.

Similar Threads

  1. How do I connect 2 method headers to make it recursive???
    By tripline in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2011, 05:27 AM
  2. Convert this to non-recursive
    By IAmHere in forum Algorithms & Recursion
    Replies: 1
    Last Post: June 10th, 2011, 11:28 AM
  3. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 03:05 PM
  4. Problem with Recursive code
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 22nd, 2010, 08:36 PM
  5. dynamic method problem...
    By Ranger-Man in forum Object Oriented Programming
    Replies: 8
    Last Post: September 7th, 2009, 04:22 PM

Tags for this Thread