简体   繁体   中英

Creating fractal pattern using recursion

I am trying to create this fractal pattern using recursion.

*
* *
  *
* * * *
    *
    * *
      *
* * * * * * * *
        *
        * *
          *
        * * * *
            *
            * *
              *

The function that I need to implement is this:

void pattern(ostream& outs, unsigned int n, unsigned int i);
 // Precondition: n is a power of 2 greater than zero.
 // Postcondition: A pattern based on the above example has been
 // printed to the ostream outs. The longest line of the pattern has
 // n stars beginning in column i of the output. For example,
 // The above pattern is produced by the call pattern(cout, 8, 0).

So far, this is what I have:

void pattern(ostream& outs, unsigned int n, unsigned int i){

    if (n == 1){
        outs << "*"<<endl;
    }

    else{
        pattern(outs, n / 2, i + 1);
        for (int k = 0; k < n; k++){
            outs << "* ";

        }
        outs<<endl;


        for (int k = 0; k < i; k++){
            outs << ' ';
        }

        pattern(outs, n / 2, i + 1);

    }

}

My code outputs what should be outputted, but the amount of spaces is off. How can I fix it?

The pattern contains 2*N-1 lines. My approach is to divide the pattern into two halves. Upper half having N lines and lower half having N-1 lines. The lower half is just the replica of upper half having one less row and few additional spaces(ie N / 2 additional spaces). So now you have to find pattern for upper half only.

To find the pattern for upper half, find the relation between number of stars and spaces with line number.

I found for N=8

LineNumber  Stars  Spaces
   1         1      0
   2         2      0
   3         1      1
   4         4      0
   5         1      2
   6         2      2
   7         1      3
   8         8      0
   9         1      4
   10        2      4
   11        1      5
   12        4      4
   13        1      6
   14        2      6
   15        1      7

Hope you will find the pattern by now looking at the above example. If not then you can refer spaces function in the code written below.

#include <iostream>
#include <cmath>
using namespace std;
bool PowerOfTwo(int n)
{
    if ((n&(n-1)) == 0 && n!=1)
        return true;
    return false;
}
int star(int n)
{
    int i=n,ans=0;
    while(i%2==0)
    {
        i/=2;
        ans++;
    }
    return pow(2,ans);
}
int spaces(int n)
{
   if(PowerOfTwo(n)==true)
        return 0;
    return (n-1)/2;
}
void printpattern(int n)
{
    int i,sp,st;
    sp = spaces(n);
    for(i=1;i<=sp;i++)
        cout<<" ";
    st = star(n);
    for(i=1;i<=st;i++)
        cout<<"* ";
}
void pattern(int n)
{
    int i,j,sp;
    for(i=1;i<=n;i++)               //Upper Half    
    {
        printpattern(i);
        cout<<endl;
    }
    sp = n/2;
    for(i=1;i<=n-1;i++)             //Lower Half
    {
        for(j=1;j<=sp;j++)
            cout<<" ";
        printpattern(i);
        cout<<endl;
    }
}
int main() 
{
    int n=8;
    pattern(n);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM