cs programmino

(30 pts) Recursive Fractals
Examine this pattern of asterisks and blanks, and write a recursive function called
pattern() that can generate patterns such as this: *
**
* ****
* **
* ********
* **
* ****
* **
*
With recursive thinking, the function needs only seven or eight lines of code (including two recursive calls). Your function prototype should look like this:
// Description:
// 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(8, 0).
// Precondition: n is a power of 2 greater than zero.
// Postcondition: A pattern based on the above example has been printed.
void pattern(int n, int i);
Hint: Think about how the pattern is a fractal. Can you find two smaller versions of the pattern within the large pattern? Here is some code that may be useful within your method:
// A loop to print exactly i spaces:
for (k = 0; k < i; k++) cout << " ";
// A loop to print n asterisks, each one followed by a space: for (k = 0; k < n; k++) cout << "* ";
Last edited on
Have fun with your homework.
You are double-posting @programnick:
http://www.cplusplus.com/forum/general/224371/#msg1026783
You could simply have "bumped up" your previous post, where you had made an intelligent start to the problem

More accurate descriptive spec here:
http://www.transtutors.com/questions/examine-this-pattern-of-asterisks-and-blanks-and-write-a-recursive-method-that-can-g-1877044.htm
or
http://www.cs.colorado.edu/~main/projects/chap09c.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void pattern( int left, int length )
{
   if ( length == 0 ) return;

   pattern( left, length / 2 );                            // "Half pattern" above

   for ( int i = 0; i < left  ; i++ ) cout << "  ";
   for ( int i = 0; i < length; i++ ) cout << "* ";        // Central string
   cout << endl;

   pattern( left + length / 2, length / 2 );               // "Half pattern" below
}

int main()
{
   int n;
   cout << "Enter n (a power of 2): ";   cin >> n;
   pattern( 0, n );
}



Or, if you prefer something other than that hinted at ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

string operator *( int n, string repeat )
{
   string result = "";
   while ( n-- ) result += repeat;
   return result;
}

string pattern( int left, int len )
{
   string BLANK = "  ", MARK = "* ";
   if ( len == 0 ) return "";
   return pattern( left, len/2 )  +  left * BLANK + len * MARK + '\n'  +  pattern( left + len/2, len/2 );
}

int main()
{
   int n;
   cout << "Enter n (a power of 2): ";   cin >> n;
   cout << pattern( 0, n );
}




Enter n (a power of 2): 8
* 
* * 
  * 
* * * * 
    * 
    * * 
      * 
* * * * * * * * 
        * 
        * * 
          * 
        * * * * 
            * 
            * * 
              *


Enter n (a power of 2): 16
* 
* * 
  * 
* * * * 
    * 
    * * 
      * 
* * * * * * * * 
        * 
        * * 
          * 
        * * * * 
            * 
            * * 
              * 
* * * * * * * * * * * * * * * * 
                * 
                * * 
                  * 
                * * * * 
                    * 
                    * * 
                      * 
                * * * * * * * * 
                        * 
                        * * 
                          * 
                        * * * * 
                            * 
                            * * 
                              * 

Last edited on
thanks all
Topic archived. No new replies allowed.