Shapes

How to make this shape??????
****
*****
******
*******
******
*****
****
The easiest way is to make a loop that outputs the number of stars for each line, inside another loop, while changing the value of the first loop as needed.

For instance, the following code can be used to output a few stars.
1
2
for (int i=4;i>0;i--)
cout<<"*";

If, instead of 4, you used a variable equal to 4 that can be changed each iteration of the loop, it should be able get it to change the number of stars for each line. The outer loop should simply contain the command to go to the next line, prior to the start of the next loop.

Here's a simple example of what I mean, using two loops. The number of times through the outer loop dictates how many stars are outputted by each execution of the inner loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int i;
    for (int n=0;n<7;n++)
    {
        //decide how many stars are needed
        if (n==0 || n==6)
            i=4;
        else if (n==1 || n==5)
            i=5;
        else if (n==2 || n==4)
            i=6;
        else
            i=7;
      //inner loop outputs desired number of stars
      for (i;i>0;i--)
        cout<<"*";

        cout<<endl;//go to next line before repeating loops
    }
}

Sample Output:
****
*****
******
*******
******
*****
****
Last edited on
You could use setfill to set a character and then replace your second for loop with the setw stream manipulator:
1
2
std::cout << std::setfill('*');
std::cout << std::setw(i) << '*' << std::endl;


You only have to use setfill once, so you can do it outside all of the loops.
http://www.cplusplus.com/reference/iomanip/setfill/
Last edited on
Now you've made me curious, yulingo. How would the setfill command be used to output a specific number of stars for each iteration of the loop?
By using a combination of setfill and setw, you specify what is to be filled and how many spaces the fill takes up. When you set the width of your output, cout will fill any extra space that isn't taken up by the printed value with the fill character. By default this is a space, but you can make it whatever you want.

So in the example above, by setting the output width to a certain value and printing a single star, it causes all the other left over space in the output width to be automatically filled with stars as well.
Thanks for the info. I had not been familiar with setfill. I was trying to demonstrate a simple way to output what was requested, but in the process I learned something as well. That's one interesting thing about these forums. In addition, I'm sure there's a way to output what was needed easier than the example I gave as well, but the interesting thing about C++ is there is no one correct answer.
No, there isn't, and honestly, in this case probably a brute-force approach of just writing a bunch of cout << ... statements would be easier to read, even though it breaks some rules about copy-pasting code.

I'm glad you learned something, and I'm sure you'll surprise me one day with something I didn't think about.
Topic archived. No new replies allowed.