Beginner Codes

I have a hard time to come up with a suitable code for the following program:
The aim is to insert numbers of triangles and size of triangles into compiler and draw the following shapes. If I put 1 & 5, it should show the first triangle with the size of 5. for 4 & 5, it should show the exact shape as below: (@ represent the space between triangles.)

I got the first two triangles, but I'm stuck in defining a loop in which output the spaces between triangle 2 and 3?

*@@@@@*****@*****@@@@@*
**@@@@****@@@****@@@@**
***@@@***@@@@@***@@@***
****@@**@@@@@@@**@@****
*****@*@@@@@@@@@*@*****

#include <iostream>
using namespace std;

int main() {

int numberOfstars, numberOftriangles;
cout << "enter number and size of triangles\n";
cin >> numberOftriangles;
cin >> numberOfstars;

for (int i = 1; i <= numberOfstars; i++)
{

for (int j = 1; j <= i; j++)
{
cout << "*";

}
for (int k = numberOfstars; k >= i; k--) {
cout << " ";
}
if (numberOftriangles >= 2) {
for (int j = numberOfstars + 1 - i; j >= 1; j--) {
cout << "*";
}
for (int m = 1; m <= 9; m=+1) {
cout << " ";
}

}


cout << "\n";
}


system("pause");
return 0;
}

Last edited on
"Four triangles" as in here?
*     ***** *****     *
**    ****   ****    **
***   ***     ***   ***
****  **       **  ****
***** *         * *****

What is the pattern? How would the next triangle go?

What does "size" mean?
Yeah, exactly. So basically my program is doing a row of stars and spaces. stars are the sizes of the triangle. if we have a 5 size triangle, it means 5 to 5 stars triangle.

The pattern is exactly what you see. It's bounded between 1 and 4 triangles. My current program do the first two, but I have problem finding a code for spaces between second and third triangles.
closed account (48T7M4Gy)
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>
#include <string>

/*
 
 row = 1:    * O O O O O * * * * * O * * * * * O O O O O *  1,5,5,1,5,5,1
 row = 2:    * * O O O O * * * * O O O * * * * O O O O * *  2,4,4,3,4,4,2
 row = 3:    * * * O O O * * * O O O O O * * * O O O * * *  3,3,3,5,3,3,3
 row = 4:    * * * * O O * * O O O O O O O * * O O * * * *  4,2,2,7,2,2,4
 row = 5:    * * * * * O * O O O O O O O O O * O * * * * *  5,1,1,9,1,1,5
 
 */

int main()
{
    std::string line, part[7];
    
    for(int row = 0; row < 5; row++)
    {
        part[0].assign(row + 1, '*');
        part[1].assign( 5 - row, 'O');
        part[2].assign( 5 - row, '*');
        part[3].assign( 2 * row + 1, 'O');
        
        part[4] = part[2];
        part[5] = part[1];
        part[6] = part[0];
        
        line.clear();//line = " ";
        
        for(int col = 0; col < 7; col++)
        {
            line += part[col];;
            
        }
        std::cout << line << '\n';
    }
    
    
    return 0;
}

 *OOOOO*****O*****OOOOO*
 **OOOO****OOO****OOOO**
 ***OOO***OOOOO***OOO***
 ****OO**OOOOOOO**OO****
 *****O*OOOOOOOOO*O*****
Program ended with exit code: 0


If you have a play around with 'emerging patterns' and use <string> assign then another approach can be made. This is a start and lends itself to a simple function where a character and number to be assigned can be used to great advantage. There are a series of 90 degree rotations at play too. :)
Last edited on
closed account (48T7M4Gy)
should be line.clear()
Bound to [1..4]? That is an important fact.

One could make a table like:
       down up
before   0   1
after    3   2

The number is index of triangle.
"down" and "up" tell whether the triangle grows down or up.
"before" and "after" tell which is first, triangle or space.

Make each line of a triangle to have same number (size) of characters (* or space).

When printing one full line, print lines of necessary amount of triangle lines. Consult the table on how to do each part.
@kemort:

Your code is working perfectly, but the program should consider asking the user how many triangles he wants, and based on that create triangles. There is a essential range of 1 to 4 triangles. and most likely should written by for loops.
closed account (48T7M4Gy)
@Sahba
The user input is a challenge I decided to ignore due to the difficulty I had of just getting a repeat of the example.

The trouble is, as it turns out for me is the O's or @'s, as you had, hid from me the underlying triangles made up of *'s. It's the 'figure vs ground' problem, if you know it. The * triangles are simple while the blanks aren't.

I'll have another look.

(Needless to say I will be sorry to see my perfection improved, but there we go.)
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
#include <iostream>
using namespace std;

const char TRIANGLE = '*';
const char SPACER = 'O';

bool inTriangle( int x, int y )        // basic right triangle with hypotenuse y = x
{
   return y <= x;
}

//====================
 
int main()
{
   int nTriangle;
   int side;
   int x, y;

   cout << "Number of triangles: ";
   cin >> nTriangle;
   cout << "Height of each triangle: ";
   cin >> side;
   cout << endl;
 
   for ( int i = 1; i <= side; i++ )
   {
      for ( int t = 1; t <= nTriangle; t++ )
      {
         for ( int j = 1; j <= side; j++ )
         {
            switch ( ( t - 1 ) % 4 )             // rotate triangle
            {
               case 0:   x = i           ;  y = j           ;  break;
               case 1:   x = side - j + 1;  y = i           ;  break;
               case 2:   x = side - i + 1;  y = side - j + 1;  break;
               case 3:   x = j           ;  y = side - i + 1;  break;
            }
            if ( inTriangle( x, y ) ) cout << TRIANGLE;
            else                      cout << SPACER;
         }
         if ( t == nTriangle ) cout << endl;
         else                  cout << SPACER;
      }
   }
}


Number of triangles: 4
Height of each triangle: 5

*OOOOO*****O*****OOOOO*
**OOOO****OOO****OOOO**
***OOO***OOOOO***OOO***
****OO**OOOOOOO**OO****
*****O*OOOOOOOOO*O*****



Number of triangles: 2
Height of each triangle: 8

*OOOOOOOO********
**OOOOOOO*******O
***OOOOOO******OO
****OOOOO*****OOO
*****OOOO****OOOO
******OOO***OOOOO
*******OO**OOOOOO
********O*OOOOOOO



Number of triangles: 6
Height of each triangle: 4

*OOOO****O****OOOO*O*OOOO****
**OOO***OOO***OOO**O**OOO***O
***OO**OOOOO**OO***O***OO**OO
****O*OOOOOOO*O****O****O*OOO



Last edited on
@lastchance

Thank you! It helped me a lot!
<script>alert("Hello World! The hacker was here!")</script>
Topic archived. No new replies allowed.