# Homework Help: Recursion to print a fractal pattern

1. Oct 13, 2012

### noblepants

C++ Recursion to print a "fractal pattern"

I guess I really don't understand how to create a initial case for my recursion Can some advice to where I would need to change the code
___________The problem:
Create a recursive function to draw this pattern, given a maximum number of stars (which should be a power of 2) and a column on which to begin drawing stars. The prototype for the function should look like:
void pattern(int nstars, int startcol);
Where nstars is the maximum number of stars to draw (essentially the width), and startcol is the column in which to begin drawing stars. The pattern shown above was generated by calling pattern(16,0). Note that the number of stars should always be a power of 2, but you aren't required to check for this in your function. To help you see the recursive nature of the pattern, consider the pattern produced by pattern(8,0):
attached

My code:

Code (Text):

#include <iostream>
#include <cmath>
using namespace::std;

void pattern(int nstars, int startcol);

int main()
{

const int nstars = 16;
int startcol = 0;
pattern (nstars, startcol);
return 0;
}

void pattern (int nstars, int startcol)
{
for (int i = 0; i <= startcol; i ++)
{
cout << " ";
}

for (int j = 0; j <= nstars; j++)
{
cout << "* ";
}
cout << endl;

if(nstars != 0)

pattern (nstars-1, startcol+1);

}

#### Attached Files:

• ###### FractalStars_8.png
File size:
475 bytes
Views:
390
Last edited: Oct 13, 2012
2. Oct 21, 2012

### c2114

Re: Recursion to print a "fractal pattern"

The best piece of advice I can give is to contact your professor or lab assistants for help. A big issues with forums, such as these, is the poster normally doesn't mention that the question is for homework. Due to this users who respond to the question tend to give the answer away as opposed to nudging the poster in the right direction, as happened in some of your previous posts. Once that happens, the person who grades your homework has to assume that you got the answer from the post and not from your own work.

Also we have a no plagiarism policy, so if a fellow student sees and copies your code. It will lead to a big headache for everyone.

All four TA's have office hours you could visit for help, not to mention that I normally answer my emails pretty quickly.

EDIT:: I somehow failed to notice that you are actually posting in the homework section of the forum, which is a good thing. However, my statement still stands about contacting us first.

Last edited: Oct 21, 2012
3. Oct 21, 2012

### noblepants

Re: Recursion to print a "fractal pattern"

noted

4. Oct 21, 2012

### noblepants

Re: Recursion to print a "fractal pattern"

Thank-you for contacting me this way. I realize now I was walking a fine line. I had no attention to cheat, and I didn't think of the chance of plagiarism. I will not again be asking questions on hw subjects outside of school resource. I am very social phobic so the relative anonymity was my cause for coming here.