Recursion to print a fractal pattern

  • Thread starter Thread starter noblepants
  • Start date Start date
  • Tags Tags
    Fractal Recursion
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 8K views
noblepants
Messages
7
Reaction score
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:

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

  • FractalStars_8.png
    FractalStars_8.png
    420 bytes · Views: 1,072
Last edited:
Physics news on Phys.org


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:


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 homework subjects outside of school resource. I am very social phobic so the relative anonymity was my cause for coming here.