Results 1 to 5 of 5
Thread: Problems with Recursion...
- 11-16-2011, 11:49 AM #1
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
- 11-16-2011, 12:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Problems with Recursion...
I answered exactly that same question yesterday here: Urgent Help Java Recursive Function Please.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2011, 01:11 PM #3
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
- 11-16-2011, 01:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2011, 03:39 PM #5
Similar Threads
-
Need help with recursion
By Playboytrey in forum New To JavaReplies: 2Last Post: 04-29-2011, 01:36 AM -
Recursion Problems. Pls Help!
By ferdzz in forum New To JavaReplies: 14Last Post: 06-17-2010, 01:26 AM -
Recursion assignment problems
By tfitz666 in forum New To JavaReplies: 11Last Post: 01-24-2010, 09:18 PM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Recursion help
By rjg_2186 in forum New To JavaReplies: 1Last Post: 01-02-2009, 08:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks