- #1
noblepants
- 7
- 0
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:
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:
#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);
}
Attachments
Last edited: