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 *
- followed by
0
spaces
- followed by one
*
- followed by
7
spaces
- followed by one
*
- followed by
7
spaces
- followed by one
*
- followed by
0
spaces
followed by one *
Line 3
consists of one *
- followed by
1
spaces
- followed by one
*
- followed by
6
spaces
- followed by one
*
- followed by
6
spaces
- followed by one
*
- followed by
1
spaces
followed by one *
.
Line 4
consists of one *
- followed by
2
spaces
- followed by one
*
- followed by
5
spaces
- followed by one
*
- followed by
5
spaces
- followed by one
*
- followed by
2
spaces
followed by one *
.
Line 5
consists of one *
- followed by
3
spaces
- followed by one
*
- followed by
4
spaces
- followed by one
*
- followed by
4
spaces
- followed by one
*
- followed by
3
spaces
followed by one *
.
Line 6
consists of one *
- followed by
4
spaces
- followed by one
*
- followed by
3
spaces
- followed by one
*
- followed by
3
spaces
- followed by one
*
- followed by
4
spaces
followed by one *
.
Line 7
consists of one *
- followed by
5
spaces
- followed by one
*
- followed by
2
spaces
- followed by one
*
- followed by
2
spaces
- followed by one
*
- followed by
5
spaces
followed by one *
.
Line 8
consists of one *
- followed by
6
spaces
- followed by one
*
- followed by
1
spaces
- followed by one
*
- followed by
1
spaces
- followed by one
*
- followed by
6
spaces
followed by one *
.
Line 9
consists of one *
- followed by
7
spaces
- followed by one
*
- followed by
0
spaces
- followed by one
*
- followed by
0
spaces
- followed by one
*
- followed by
7
spaces
followed 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 *
- followed by
n-2
spaces
- followed by one
*
- followed by
(size/2) - n
spaces
- followed by one
*
- followed by
(size/2) - n
spaces
- followed by one
*
- followed by
n-2
spaces
followed 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
.
In accordance with the community guidelines for homework questions, I will not provide the full solution at this time. I can provide further information later, if necessary.
EDIT:
Since several other solutions have already been posted by other users, 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' );
}
}