Stars pyramid

So I'm having trouble with this program. I have to print all the variations (check the image: https://gyazo.com/c28fec509d5647d8f179a55c29ac4d91 ). I've completed (a and (b, but I have no idea about (c and (d. Can you help out, please?


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
46
47
#include <iostream>
using namespace std;

int main()

// 1

{
	cout << "Pyramid 1" << endl;

	int line1 = 1;
	do
	{
	int stars1 = 1;
		do
		{
			cout << "*";
			stars1++;
		} while (stars1 <= line1);
		cout << endl;
		line++;
	} while (line <= 10);

	// 2
	cout << "Pyramid 2" << endl;

	int stars2 = 10;
	int line2 = 1;
	while (stars2 != 0)
	{
		while (stars2 >= red2)
		{
			cout << "*";
			line2++;
		}
		stars2--;
		line2 = 1;
		cout << endl;
	}

	// 3

	cout << "Pyramid 3" << endl;



	return 0;
Last edited on
On the first two you did print some '*' to a line.
On the last two you have to first print some ' ' and then some '*' to a line.
Thanks, man! Will try it.
Still having problems, can show me how the code is supposed to look like? Thanks in advance.
First, all three loops: for, while and do while can be used:
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>

int main()
{
    const int N {10};

    for ( int row = 0; row < N; ++row ) {
        for ( int col = 0; col <= row; ++col ) {
            std::cout << '*';
        }
        std::cout << '\n';
    }

    std::cout << "----\n";

    for ( int row = 0; row < N; ++row ) {
        int col {0};
        while ( col < 7 ) {
            std::cout << '=';
            ++col;
        }
        while ( col < N ) {
            std::cout << '*';
            ++col;
        }
        std::cout << '\n';
    }

    return 0;
}

Second, pay attention to lines 17-26. They print one line. The first loop advances the 'col' to 7 (and prints 7 characters) and the second loop advances the 'col' al the way to N (printing N-7 characters). Each line prints N characters.

You obviously do not want to print 7 whitespace and N-7 * on each line. You have to replace 7 with something more appropriate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void draw( int rows, int type )
{
   cout << "\n\nPyramid " << type << ":\n";
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < rows; j++ )
      {
         cout << (  (type%2 ? i-j : rows-1-i-j) * (type>2 ? -1 : 1) >= 0 ? '*' : ' ' );
      }
      cout << '\n';
   }
}

int main()
{
   for ( int type = 1; type <= 4; type++ ) draw( 10, type );
}



Pyramid 1:
*         
**        
***       
****      
*****     
******    
*******   
********  
********* 
**********


Pyramid 2:
**********
********* 
********  
*******   
******    
*****     
****      
***       
**        
*         


Pyramid 3:
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *


Pyramid 4:
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********
Last edited on
Topic archived. No new replies allowed.