# help with recursion function

• 12-10-2006
robasc
help with recursion function
I am trying to create a shape in the form of:

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

I got this shape:

****
***
**
*
using a recursion function but I have been beating myself in the head trying to figure out the rest of it:

Any clues?
Code:

```#include <cstdlib> #include <iostream> using namespace std; int design(int); int main(int argc, char *argv[]) {     design(4);     system("PAUSE");     return EXIT_SUCCESS; } int design(int x) { int i; int j = 3;   for(i =0; i < x; i++)                cout << "*";                        cout << endl;                      if(x == 0)                 return 1;         else         design(x - 1);```
program output so far is:

****
***
**
*
• 12-10-2006
manutd
If x equals zero, set a variable like goingup to TRUE, call design(x+1) and test in the function for goingup. If it is TRUE increment x and call design(x+1).
• 12-10-2006
robatino
You don't need recursion. You can do what you've already done with a simple double loop. Then write another double loop after the first one to output the second half of the pattern.
• 12-10-2006
robasc
yes I do realize that but I am trying to learn how to do it using a single recursive function.

But hey thanks for the reply.
• 12-10-2006
robasc
well I got some what of an idea but my inner if statement does not increment back up.
Code:

```#include <cstdlib> #include <iostream> using namespace std; int design(int); int main(int argc, char *argv[]) {     design(4);     system("PAUSE");     return EXIT_SUCCESS; } int design(int x) { int i; int j = 3;   for(i =0; i < x; i++)  //run until x = 0    {           cout << "*";          }        cout << endl;                      if(x == 0)         {                                 for(i = 0; i <= x; i++) // run until x = 4                                                 {                                                         cout << "*";                                                                        }                                                 cout << endl;                                         if(x == 4)                                         {                                           return 1;                                         }                                     else                                         {                                       design(x + 1);  // here my x does not add to one                                         }         }         else         {         design(x - 1);         }```
• 12-10-2006
Tonto
Another thing could do, without the whole concept of this 'direction' stuff (which is really more iterative than disguised as recursive), is something like this.

Code:

```void design(int x) {         // print stars         // call design(x-1)         // print stars }```
• 12-10-2006
robatino
For this particular problem, whether you use recursion or not, the most natural way to do it is to have two different pieces of code to handle the two halves of the pattern. If you use recursion, it's easier to have 2 recursive functions, one for each half. And you already know how to do that.
• 12-10-2006
Tonto
>> For this particular problem, whether you use recursion or not, the most natural way to do it is to have two different pieces of code to handle the two halves of the pattern. If you use recursion, it's easier to have 2 recursive functions, one for each half. And you already know how to do that.

False: see, my post. Only um -- bipartisan [?] divided into two parts [?] in the sense that it does the same printing twice.
• 12-10-2006
robatino
Yes, you're right. Assuming this is a classroom problem, that's probably what the instructor was thinking of, in fact.
• 12-10-2006
robasc
Well guys I finally got it:
Code:

```#include <cstdlib> #include <iostream> using namespace std; void designDown(int); int designUp(int); int main(int argc, char *argv[]) {     designDown(4);             system("PAUSE");     return EXIT_SUCCESS; } void designDown(int x) {         int i;         for(i = 0; i < x; i++)                 cout << "*";             cout << endl;                       if(x == 1)                   designUp(x);                     else                 designDown(x-1); } int designUp(int x) {   int i ;         for(i = 0; i < x; i++)                 cout << "*";             cout << endl;                 if(x == 4)                         return 1;                 else                         designUp(x + 1); }```
• 12-10-2006
robatino
Look at Tonto's code snippet - you can do this much more compactly with a single (recursive) function. And contrary to what I said earlier, it really is the most natural way to solve this particular problem.
• 12-10-2006
robasc
yes you are right! and it is much easier to understand as well.

thanks guys!!!
• 12-10-2006
anon
The whole exercise is probably about understanding recursion, in particular what happens if you do things before and after calling the function recursively. So, using two functions is not a good idea. You draw the first half of the image as you go deeper in the recursion. You draw the second half as you've reached the "bottom" and are backing out of the recursion.

Here's an example
Code:

```void design(int n) {     draw_stars(n); //function to draw a line of n stars     if (n > 1)         design(n-1);     draw_stars(n); }```
Now, this draws the line with the single star twice. See if you can make it draw it only once (hint: n==1 will be a special case).