Having trouble duplicating a hexagon array in another array horizontally

I made a simple array to produce a hexagon-like shape within another array. However, everytime I output the array it come out vertical. I need to duplicate the hexagon horizontally. I know that the cout << endl; is at fault, but I can't find an alternative for it.

Here my code:
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
#include<iostream>
using namespace std;

int main()
{
	for (int cc = 0; cc < 4; cc++){
		for (int i = 0, k = 2, l = 3; i < 2; i++, k--, l++)
		{
			for (int j = 0; j < 6; j++)
			{
				if (j >= k && j <= l)
					cout << "A";
				else
					cout << " ";
			}
			cout << endl;;
		}
		for (int i = 0, k = 1, l = 4; i < 2; i++, k++, l--)
		{
			for (int j = 0; j < 6; j++)
			{
				if (j >= k && j <= l)
					cout << "C";
				else
					cout << " ";
			}
			cout << endl;
		}
	}
	
	int a;
	cin >> a;
}

One way of doing it is to actual output a RECTANGULAR ARRAY of chars (or simply an array of strings), most of whose characters are blank, except for the C and A positions that you have at present. Get it working with the arrangement you have at present, then simply transpose row and column indices to get it the other way round.
I'm confuse, you want me to enact the transpose in the hexagon array or in the duplication array?
You could either put one hexagon in a rectangular array and print four copies of that rectangle horizontally, or you could put all hexagons into one array and transpose that.

It depends what you actually want your output to look like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    for (int i = 0, k = 2, l = 3; i < 2; i++, k--, l++) {
        for (int cc = 0; cc < 4; cc++)
            for (int j = 0; j < 6; j++)
                cout << (j >= k && j <= l ? 'A' : ' ');
            cout << '\n';
    }
    for (int i = 0, k = 1, l = 4; i < 2; i++, k++, l--) {
        for (int cc = 0; cc < 4; cc++)
            for (int j = 0; j < 6; j++)
                cout << (j >= k && j <= l ? 'C' : ' ');
            cout << '\n';
    }
}

I'm assuming that when the OP says "array" he actually means "loop". (And the hexagons look more like octagons to me!)
Last edited on
I was thinking more like this:
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
48
49
#include <iostream>
#include <string>
#include <vector>
using namespace std;

//==========================================================

vector<string> octagon( int side, char top, char bottom )           // side should be even
{
   int height = 3 * side;
   vector<string> result( height );
   for ( int i = 1, letters = side; i <= side; i++, letters += 2 )
   {
      string blanks( side - i + 1, ' ' );
      result[i             ] = blanks + string( letters, top    ) + blanks;
      result[height - 1 - i] = blanks + string( letters, bottom ) + blanks;
   }
   for ( int i = side + 1; i <= side + side / 2 - 1; i++ )
   {
      result[i             ] = result[side         ];
      result[height - 1 - i] = result[height-1-side];
   }
   return result;
}

//==========================================================

void wallpaper( string title, const vector<string> &shape, int vertical, int horizontal )
{
   int height = shape.size();
   cout << title << '\n';
   for ( int v = 0; v < vertical; v++ )
   {
      for ( int i = 0; i < height; i++ )
      {
         for ( int h = 0; h < horizontal; h++ ) cout << shape[i];
         cout << '\n';
      }
   }
}

//==========================================================

int main()
{
   wallpaper(     "4 octagons vertically"  , octagon( 2, 'A', 'C' ), 4, 1 );
   wallpaper( "\n\n4 octagons horizontally", octagon( 2, 'A', 'C' ), 1, 4 );
   wallpaper( "\n\n2 x 3 large octagons"   , octagon( 4, '*', 'O' ), 2, 3 );
}


4 octagons vertically

  AA  
 AAAA 
 CCCC 
  CC  


  AA  
 AAAA 
 CCCC 
  CC  


  AA  
 AAAA 
 CCCC 
  CC  


  AA  
 AAAA 
 CCCC 
  CC  



4 octagons horizontally

  AA    AA    AA    AA  
 AAAA  AAAA  AAAA  AAAA 
 CCCC  CCCC  CCCC  CCCC 
  CC    CC    CC    CC  



2 x 3 large octagons

    ****        ****        ****    
   ******      ******      ******   
  ********    ********    ********  
 **********  **********  ********** 
 **********  **********  ********** 
 OOOOOOOOOO  OOOOOOOOOO  OOOOOOOOOO 
 OOOOOOOOOO  OOOOOOOOOO  OOOOOOOOOO 
  OOOOOOOO    OOOOOOOO    OOOOOOOO  
   OOOOOO      OOOOOO      OOOOOO   
    OOOO        OOOO        OOOO    


    ****        ****        ****    
   ******      ******      ******   
  ********    ********    ********  
 **********  **********  ********** 
 **********  **********  ********** 
 OOOOOOOOOO  OOOOOOOOOO  OOOOOOOOOO 
 OOOOOOOOOO  OOOOOOOOOO  OOOOOOOOOO 
  OOOOOOOO    OOOOOOOO    OOOOOOOO  
   OOOOOO      OOOOOO      OOOOOO   
    OOOO        OOOO        OOOO    

Last edited on
Just for fun:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

struct duplicator
{
    explicit duplicator( int cnt, int width, int gap = 4, std::ostream& stm = std::cout )
        : cnt(cnt), width(width), gap(gap), out_stm(stm) {}

    template < typename T > duplicator& operator<< ( const T& v )
    { str_stm << v ; return *this ; }

    duplicator& operator<< ( char c )
    {
        if( c == '\n' ) return dup_line() ;
        else { str_stm << c ; return *this ; }
    }

    duplicator& operator<< ( const char* cstr )
    {
        while(*cstr) *this << *cstr++ ;
        return *this ;
    }

    duplicator& operator<< ( const std::string& str )
    {
        for( char c : str ) *this << c ;
        return *this ;
    }

    duplicator& dup_line()
    {
        const std::string str = str_stm.str() ;
        for( int i = 0 ; i < cnt ; ++i )
            out_stm << std::setw(width) << str << std::string( gap, ' ' ) ;
        out_stm << '\n' << std::flush ;
        str_stm.str( {} ) ;
        return *this ;
    }

    duplicator& operator<< ( std::ostream& (*manip)( std::ostream& ) )
    {
        if( manip == &std::endl< char, std::char_traits<char> > ) return dup_line() ;
        else { str_stm << manip ; return *this ; }
    }

    // TO DO: add support for manipulators with arguments

    int cnt ;
    int width ;
    int gap ;
    std::ostream& out_stm ;
    std::ostringstream str_stm ;
};

int main()
{
    // duplicate 5 times in fields of width 12 to stdout
    duplicator dup_cout( 5, 12 ) ;

    // print one copy of the octagon to dup_cout (it will duplicate it 5 times to stdout)
    dup_cout << " AAA " << '\n' << "AAAAA" << std::endl
             << "CCCCC\n" << " CCC " << std::endl ;
}

http://coliru.stacked-crooked.com/a/416567c9ee9fbaec
Thanks
Topic archived. No new replies allowed.