• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Help with recursion

Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this program to code but my output is only different because some of the lines have too little spaces before them.

Here is the problem
A Fractal Pattern
Examine this pattern of asterisks and blanks, and write a recursive function that can generate patterns such as this:

With recursive thinking, the function needs only seven or eight lines of code (including two recursive calls). Your prototype should look like this:

String pattern(String outs, int n, int i);
// Precondition: n is a power of 2 greater than zero.
// Postcondition: A pattern based on the above example has been
// printed to the String outs. The longest line of the pattern has
// n stars beginning in column i of the output. For example,
// The above pattern is produced by the call pattern(out, 8, 0).

Hints: You do not need to check the precondition. Think about how the pattern is a fractal. Can you find two smaller versions of the pattern within the large pattern? Here is some code that may be useful within your function:

// A loop to print exactly i spaces:
for (k = 0; k < i; k++) outs += ' ';

// A loop to print n asterisks, each one followed by a space:
for (k = 0; k < n; k++) outs += "* ";

Here is my code

My output looks like this

[ November 25, 2005: Message edited by: Marilyn de Queiroz ]
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: NeverMind.. now I see how your output is different from the required output. It looks like you need to somehow add an extra space in there...
[ November 27, 2005: Message edited by: C Clites ]
    Bookmark Topic Watch Topic
  • New Topic