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
#include<iostream>
usingnamespace 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;
}*/
}
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.
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
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
#include<iostream>
usingnamespace 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 !!!
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.
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
#include<iostream>
usingnamespace 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;