triangle bumps

hi, I am trying to write a code that prints the following bumbs when the function void Ascii::printBumps(int num, char symbol1, char symbol2)is called with (4,'%','#').


%
#
%
% %
# #
#
%
% %
% % %
# # #
# #
#
%
% %
% % %
% % % %
# # # #
# # #
# #
#


so far i have figured out how to print the triangles side by side by using
for(int i = width;i>=1;i--){

for(int j = 1;j<i;j++){
cout<<" ";
}
for(int k = width; k>=i; k--){
cout<<symbol<<" ";
}
cout<<endl;
}
for(int i = 1;i<=width-i;i++){
for(int j = 1;j<=i;j++){
cout<<" ";
}
for(int k = width-1 ; k>=i;k--){
cout<<symbol<<" ";
}
cout<<endl;
}
i cannot however figure out how to print the bumps and how to get the triangle size to increase each time.
Last edited on
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>

void print_upper_triangle( int n, char c )
{
    if( n > 1 ) print_upper_triangle( n-1, c ) ;

    for( int i = 0 ; i < n ; ++i ) std::cout << c ;
    std::cout << '\n' ;
}

void print_lower_triangle( int n, char c )
{
    for( int i = 0 ; i < n ; ++i ) std::cout << c ;
    std::cout << '\n' ;

    if( n > 1 ) print_lower_triangle( n-1, c ) ;
}

void print_bumps( int n, char symbol1, char symbol2 )
{
    if( n > 1 ) print_bumps( n-1, symbol1, symbol2 ) ;

    print_upper_triangle( n, symbol1 ) ;
    print_lower_triangle( n, symbol2 ) ;
}

int main()
{
    print_bumps( 5, 'X', 'Y' ) ;
}

http://coliru.stacked-crooked.com/a/c0336a8585d9c536
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void row( int n, char symbol )
{
   while ( n-- ) cout << symbol << ' ';
   cout << '\n';
}

void bumps( int number, char symbol1, char symbol2 )
{
   for ( int n = 1; n <= number; n++ )
   {
      for ( int i = 1; i <= n; i++ ) row( i, symbol1 );
      for ( int i = n; i >= 1; i-- ) row( i, symbol2 );
   }
}

int main()
{
   bumps( 4, '%', '#' );
}


% 
# 
% 
% % 
# # 
# 
% 
% % 
% % % 
# # # 
# # 
# 
% 
% % 
% % % 
% % % % 
# # # # 
# # # 
# # 
# 

Last edited on
Topic archived. No new replies allowed.