1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Recursion to print a fractal pattern

  1. Oct 13, 2012 #1
    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:

    Last edited: Oct 13, 2012
  2. jcsd
  3. Oct 21, 2012 #2
    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
  4. Oct 21, 2012 #3
    Re: Recursion to print a "fractal pattern"

    noted
     
  5. Oct 21, 2012 #4
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Recursion to print a fractal pattern
  1. Not printing (Replies: 4)

  2. Recursive function (Replies: 16)

  3. Recursion Tree (Replies: 3)

  4. Recursive calls (Replies: 15)

Loading...