Recursion to print a fractal pattern

In summary, the conversation is about creating a recursive function to draw a fractal pattern in C++. The function should have a maximum number of stars and a starting column as parameters. The user is seeking advice on how to create an initial case for the recursion and is reminded to seek help from their professor or lab assistants. The importance of avoiding plagiarism is also emphasized.
  • #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:

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: 969
Last edited:
Physics news on Phys.org
  • #2


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:
  • #3


noted
 
  • #4


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.
 
  • #5






I would first like to commend you for taking on the challenge of creating a recursive function to draw a fractal pattern. Recursion can be a tricky concept to grasp, but once you understand it, it can be a powerful tool in problem-solving.

Looking at your code, I can see that you have already made some progress. Your function takes in two parameters, nstars and startcol, which are essential for creating the pattern. However, as you mentioned, you are struggling with creating an initial case for your recursion.

In order to create an initial case for your recursion, you need to think about what the base case should be. In this case, the base case would be when the number of stars (nstars) reaches 0. This means that the function should stop calling itself and simply return.

To incorporate this into your code, you can add an if statement at the beginning of your function that checks if nstars is equal to 0. If it is, then the function should simply return. This will serve as your base case and prevent your function from calling itself infinitely.

I hope this helps guide you in the right direction. Remember, recursion can be challenging, but with practice and careful thinking, you will be able to solve this problem. Good luck!
 

1. What is recursion and how does it relate to fractal patterns?

Recursion is a programming technique where a function calls itself repeatedly until a specific condition is met. In the context of fractal patterns, recursion is used to create self-similar structures by repeating a specific pattern on a smaller scale within itself.

2. How is recursion used to print a fractal pattern?

In order to print a fractal pattern using recursion, a function is written that calls itself with smaller and smaller inputs until a base case is reached. Each time the function is called, it draws a smaller version of the pattern and then calls itself again with the smaller input. This process continues until the base case is reached and the entire fractal pattern is drawn.

3. What is a base case and why is it important in recursion for fractal patterns?

A base case is the stopping condition for a recursive function. In the context of fractal patterns, the base case is usually when the pattern has been drawn to a specific level of detail or when it has reached a certain size. Without a base case, the recursive function would continue to call itself infinitely and result in an error.

4. Can any fractal pattern be created using recursion?

Yes, any fractal pattern can be created using recursion as long as the pattern has self-similar characteristics and can be broken down into smaller versions of itself. However, some fractal patterns may require more complex recursive algorithms to achieve the desired level of detail.

5. Are there any drawbacks to using recursion for printing fractal patterns?

One potential drawback of using recursion for printing fractal patterns is that it can be memory intensive, as each recursive call creates a new stack frame. This can slow down the program and potentially lead to memory overflow if the pattern is drawn to a very detailed level. Additionally, writing and debugging recursive algorithms can be more challenging compared to iterative methods.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
11K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top