drawing a Shape using RECURSION as a mirrored shape.

i need to draw a shape using recursion and it must be like a mirrored shape
like this
****
***
**
*
**
***
****
i knowe how to do it with for loops but not in recursion
here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
using namespace std;
void drawShape(int n ,char s)//i try to do it but it just print the value ****
{
	 if ( n == 1 )
    
		cout<<s;
	 else
	 {
		 drawShape(n-1,s);
	 cout<<s;
	 }
void main()
{
   int n ;
   cout<<"Please Enter The Number Of The Stars"<<endl;
   cin>>n;
   char star='*';
   drawShape(n,'*');
   cout<<endl;
    /* int line, loop; //here i how i used the for loop to do it XD
     for (line = 1; line <= n; line++)
     {
          for (loop = line; loop<= n; loop++)
               cout << "*";
          cout << endl;
     }
	  for (line = 1; line <= n; line++)
     {
          for (loop = 1; loop<= line; loop++)
               cout << "*";
          cout << endl;
	  }*/
}

just need some hint from you ...thanks :)
Last edited on
You are heading in the right direction. You should be able to use your existing program, it just needs a bit of additional code.

What is missing is a newline or endl to separate the output generated by each call.
In addition, rather than a single asterisk, you need to output n asterisks. That will be the top line of the pattern. Then the recursive function call. Lastly output another n asterisks. That will be the bottom line of the pattern.
i tried everything but it wont work please i need some help ?!!
Please show your latest code, to see whether you are getting closer to a solution.
here is my new code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;
void drawShape(int n ,char s)
{
	 if ( n == 1 )
	 {
		 
		cout<<s;
	 }
	 else
	 {
		 drawShape(n-1,s);
		 drawShape(n-1,s);
	 cout<<s<<endl;
	 
	 }
}

void main()
{
   int n ;
   cout<<"Please Enter The Number Of The Stars"<<endl;
   cin>>n;
   char star='*';
   drawShape(n,'*');
   cout<<endl;
    /* int line, loop;
     for (line = 1; line <= n; line++)
     {
          for (loop = line; loop<= n; loop++)
               cout << "*";
          cout << endl;
     }
	  for (line = 1; line <= n; line++)
     {
          for (loop = 1; loop<= line; loop++)
               cout << "*";
          cout << endl;
	  }*/
}

but it print this
***
***
*
***
***
*
*
what should i do !!!
The simple stuff first. There's an endl missing from line 8.
There are two recursive calls to the function on lines 12 and 13, you only need one of them.

Now, line 14. You need to expand this, using perhaps a for loop, or maybe something like cout << std::string (n, s) << endl;, whatever your preference. The aim is to print n consecutive stars, on the same line, followed by the endl. Do it whichever way you prefer.

You also need the above code inserting above the function call too.

In other words you will have, after the else at line 10:
1
2
3
    print n stars + newline
    call the function with (n-1)
    print n stars + newline


Hope this helps a bit.
oh ..thanks i tried but it doesn't work neither...
it print



*



just this star in the middle with this space above and below iwrote just like you told me... so what should i do ??I know I asked you a lot but i need to solve it....please help me
What does your code look like now? We can't advise on something that's not shown.
ok i tried a several thing looke how my cod now and what is the out put
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;
void drawShape(int n ,char s)
{
	


	 if ( n == 1 )
	 {
		 
		cout<<s;
	 }
	 else
	 {
		
		cout<<s;
		drawShape(n-1,s);
		cout<<s<<endl;
	 drawShape(n-1,s);
	 }
}

void main()
{
   int n ;
   cout<<"Please Enter The Number Of The Stars"<<endl;
   cin>>n;
   char star='*';
   drawShape(n,'*');
   cout<<endl;
    /* int line, loop;
     for (line = 1; line <= n; line++)
     {
          for (loop = line; loop<= n; loop++)
               cout << "*";
          cout << endl;
     }
	  for (line = 1; line <= n; line++)
     {
          for (loop = 1; loop<= line; loop++)
               cout << "*";
          cout << endl;
	  }*/
}

when i enter the number of the stars like 4 it print
****
**
***
**
****
**
***
**
and i need to print this when i entered 4
****
***
**
*
**
***
****
im hopeless !!!
or is there any other way to solve it ....i need any way just any way im really hopeless
Thank you for sharing your latest code - this is helpful.

Well, I don't know how to explain this without actually writing the code for you. But the above code doesn't match any of the instructions I gave last time.

Last time I said this: "There are two recursive calls to the function on lines 12 and 13, you only need one of them."
But what do I see now? Two calls of the function, this time on lines 17 and 19.

Seriously, you didn't follow any of the instructions, I think I should give up.

Please read and pay attention to my earlier post. http://www.cplusplus.com/forum/beginner/85159/#msg456850
hi again
i asked my teacher today and she told me you have to use one for loop and to draw
****
***
**
*
and then use the recursion ti draw
*
**
***
****
ok i told you i now how to do the first one and this is my code i have a problem but i dont know where the recursion is not working
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;
void drawShape(int n ,char s)
{
	
     
	  if ( n == 1)
	{	 cout<<s;
	  }
	  else
		{ 
		drawShape(n/2,s); 
		cout<<s;
	  }
}
void main()
{
   int n ;
   cout<<"Please Enter The Number Of The Stars"<<endl;
   cin>>n;
   char star='*';
   int line, loop;
     for (line = 1; line <= n; line++ )
     {
          for (loop = line; loop<= n; loop++)
               cout << "*";
          cout << endl;
	 }
   drawShape(n,'*');
   cout<<endl;

Topic archived. No new replies allowed.