Results 1 to 5 of 5
  1. #1
    AmazingDude's Avatar
    AmazingDude is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Problems with Recursion...

    Hi.
    I've got small problem with my recursive function. My task sounds like this:

    A builder wishes to build a (three-dimensional) pyramid. Each layer is
    comprised of square blocks. The top layer comprises a single block, the next
    layer comprises four blocks (2 x 2 blocks). The next expands so that each side
    is 3 blocks long. The central block is not inserted so the layer comprises 8
    blocks. Each additional layer adds another block to the edge and only lays the
    outer blocks.

    1. Write a series of tests in Q3Test that would ensure a function calculated
    the correct number of blocks required based on the above specification.
    Document (using comments) the test cases that you are going to use.
    2. Write a recursive function that will perform the calculations and run your
    tests against the method

    Problem is with second question. My code seems not working correctly and i can't determine the problem. I am posting my code bellow:

    public int PBR( int layersN ) {
    if (layersN < 1)
    return 0;
    else
    return layersN == 1 ? 1 : 2*PBR( layersN-1 )-PBR( layersN-2 )+4;
    }

    When i run test and make layersN = 3, I expect answer 13, but BlueJ gives me answer 15. Cant determine where is mistake so I would be really grateful if somebody will help me.

    Thx
    AmazingDude

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with Recursion...

    I answered exactly that same question yesterday here: Urgent Help Java Recursive Function Please.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    AmazingDude's Avatar
    AmazingDude is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Problems with Recursion...

    Yes i know and I checked that post already, but there is no answer to my question. I would like to know why my function is not working, i don't need to know why another persons function problem. All what I need is to find a place in function which cause mistake, and i just cant see it... This is my question.

    Thx
    AmazingDude

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with Recursion...

    Not just layersN == 1 is an exception to the rule, also layersN == 2 is an exception, because that layer doesn't contain four more bricks than the previous layer. See my version of the code in the other thread. (I started at layersN == 0, you start at layersN == 1)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    AmazingDude's Avatar
    AmazingDude is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Problems with Recursion...

    Indeed mate, you right... How could I miss that...

    Thx for the help.
    Amazing dude

Similar Threads

  1. Need help with recursion
    By Playboytrey in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 01:36 AM
  2. Recursion Problems. Pls Help!
    By ferdzz in forum New To Java
    Replies: 14
    Last Post: 06-17-2010, 01:26 AM
  3. Recursion assignment problems
    By tfitz666 in forum New To Java
    Replies: 11
    Last Post: 01-24-2010, 09:18 PM
  4. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  5. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 08:03 AM

Posting Permissions

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