This is the flag that I have to get at the end:
*******************
** * **
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* *** *
*******************
* *** *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
** * **
*******************
I know how to do full star triangles but when it's empty on the inside I have no idea about how to proceed. Can anyone help me?
I tried and I just know how to do full star triangles and a star square/rectangle empty on the inside here is the code:
int main(void)
{
int i, j, length, width;
cout << "Length of rectangle? ";
cin >> length;
cout << endl;
cout << "Width of rectangle? ";
cin >> width;
cout << endl;
for ( i = 0; i < length; i++ )
cout << "*";
cout << endl;
for ( i = 1; i < width - 1; i++ )
{
cout << "*";
for ( j = 1; j < length - 1; j++ )
{
cout << " ";
}
cout << "*";
cout << endl;
}
for ( i = 0; i < length; i++)
cout << "*";
cout << endl;
return 0;
}
Lines 1
, 10
and 19
are easy, as they each consist only of 19 *
.
The problem is the lines 2
to 9
and 11
to 19
.
However, do you notice a pattern in lines 2
to 9
?
Line 2
consists of one *
0
spaces*
7
spaces*
7
spaces*
0
spacesfollowed by one *
Line 3
consists of one *
1
spaces*
6
spaces*
6
spaces*
1
spacesfollowed by one *
.
Line 4
consists of one *
2
spaces*
5
spaces*
5
spaces*
2
spacesfollowed by one *
.
Line 5
consists of one *
3
spaces*
4
spaces*
4
spaces*
3
spacesfollowed by one *
.
Line 6
consists of one *
4
spaces*
3
spaces*
3
spaces*
4
spacesfollowed by one *
.
Line 7
consists of one *
5
spaces*
2
spaces*
2
spaces*
5
spacesfollowed by one *
.
Line 8
consists of one *
6
spaces*
1
spaces*
1
spaces*
6
spacesfollowed by one *
.
Line 9
consists of one *
7
spaces*
0
spaces*
0
spaces*
7
spacesfollowed by one *
.
The pattern is the following:
Assuming that size
is the total size of the triangle (which is 19
in your case), then
line n
consists of one *
n-2
spaces*
(size/2) - n
spaces*
(size/2) - n
spaces*
n-2
spacesfollowed by one *
.
Note that in C, the result of 19 / 2
is 9
, as the fractional part of the division is discarded.
Using this information about the pattern, you should be able to create a loop that in every loop iteration, prints one line as described above. That way, you should be able to solve the problem of printing the lines 2
to 9
.
Printing the lines 11
to 19
should be easy afterwards, because these lines must only be printed in reverse order of the lines 2
to 9
.
I will now also post my solution, which solves the problem as described above:
#include <iostream>
const int MAP_SIZE = 19;
static_assert( MAP_SIZE % 2 == 1, "MAP_SIZE must be odd" );
int main( void )
{
//print first horizontal line
for ( int i = 0; i < MAP_SIZE; i++ )
std::cout << '*';
std::cout << '\n';
//print top half of flag
for ( int i = 0; i < MAP_SIZE / 2 - 1; i++ )
{
std::cout << '*';
for ( int j = 0; j < i; j++ )
std::cout << ' ';
std::cout << '*';
for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
std::cout << ' ';
std::cout << '*';
for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
std::cout << ' ';
std::cout << '*';
for ( int j = 0; j < i; j++ )
std::cout << ' ';
std::cout << '*';
std::cout << '\n';
}
//print second horizontal line
for ( int i = 0; i < MAP_SIZE; i++ )
std::cout << '*';
std::cout << '\n';
//print bottom half of flag
for ( int i = 0; i < MAP_SIZE / 2 - 1; i++ )
{
std::cout << '*';
for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
std::cout << ' ';
std::cout << '*';
for ( int j = 0; j < i; j++ )
std::cout << ' ';
std::cout << '*';
for ( int j = 0; j < i; j++ )
std::cout << ' ';
std::cout << '*';
for ( int j = 0; j < MAP_SIZE/2 - 2 - i; j++ )
std::cout << ' ';
std::cout << '*';
std::cout << '\n';
}
//print third horizontal line
for ( int i = 0; i < MAP_SIZE; i++ )
std::cout << '*';
std::cout << '\n';
}
However, I think that this problem is easier to solve using a 2D array (which you stated that you are not allowed to use). The 2D array is initialized to spaces and then the 3 horizontal, 3 vertical and 2 diagonal lines are drawn:
#include <iostream>
const int MAP_SIZE = 19;
static_assert( MAP_SIZE % 2 == 1, "MAP_SIZE must be odd" );
int main( void )
{
char map[MAP_SIZE][MAP_SIZE];
//initialize 2D array to spaces
for ( int i = 0; i < MAP_SIZE; i++ )
for ( int j = 0; j < MAP_SIZE; j++ )
map[i][j] = ' ';
//draw the 3 horizontal lines
for ( int i = 0; i < MAP_SIZE; i++ )
{
map[ 0][i] = '*';
map[MAP_SIZE/2][i] = '*';
map[MAP_SIZE-1][i] = '*';
}
//draw the 3 vertical lines
for ( int i = 0; i < MAP_SIZE; i++ )
{
map[i][ 0] = '*';
map[i][MAP_SIZE/2] = '*';
map[i][MAP_SIZE-1] = '*';
}
//draw the 2 diagonal lines
for ( int i = 0; i < MAP_SIZE; i++ )
{
map[i][ i] = '*';
map[i][MAP_SIZE-i-1] = '*';
}
//print the result
for ( int i = 0; i < MAP_SIZE; i++ )
{
std::cout.write( map[i], MAP_SIZE );
std::cout.put( '\n' );
}
}