pattern printing

how to print this type of pattern
in this example n is given 5
and the pattern forms with 1's and 0's
outermost is made of all 1's next inner is made of all 0's and next one is all 1's and it goes on in such pattern then following is the output
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
   string sym = "10";
   int n;
   cout << "Input n: ";   cin >> n;
   vector<string> canvas( n, string( 2 * n - 1, ' ' ) );
   int T = ( n + 1 ) / 2;
   for ( int t = 0; t < T; t++ )
   {
      for ( int i = t; i < n - t; i++ ) canvas[i][n-1+i-t]=canvas[i][n-1-i+t] = sym[t%2];
      for ( int j = 2 * t; j < 2 * n - 1 - 2 * t; j++ ) canvas[n-1-t][j] = sym[t%2];
   }
   for ( string &s : canvas ) cout << s << '\n';
}


Input n: 5
    1    
   101   
  10101  
 1000001 
111111111



Input n: 10
         1         
        101        
       10101       
      1010101      
     101010101     
    10101110101    
   1010000000101   
  101111111111101  
 10000000000000001 
1111111111111111111
Topic archived. No new replies allowed.