rectangle with ziggzagg lines-need help...

i m having a problem with getting the right equation for my code (if i replace the 8 with n). sry for my english.

#include <iostream>

using namespace std;

int main()
{

for(int x=1;x<=3;x++){
for(int y=1;y<8;y++){
if((x==3&&y==1)||(x==1&&y==3)||(x==2&&y==2)||(x==3&&y==5)||(x==1&&y==7)||(x==2&&y==4)||(x==2&&y==6)||(x==2&&y==8)){
cout<<"*";
}
else cout<<"#";
}
cout<<endl;
}
}
How should it look like ?
##*###*###*###*##
#*#*#*#*#*#*#*#*#
*###*###*###*###*
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
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

const int  IMAX       = 3  ;      // down
const int  JMAX       = 17 ;      // across
const char BACKGROUND = '#';
const char FOREGROUND = '*';


int main()
{
   int i, j;                               

   // Flood with background
   char screen[IMAX][JMAX];
   for ( i = 0; i < IMAX; i++ )
   {
      for ( j = 0; j < JMAX; j++ ) screen[i][j] = BACKGROUND; 
   }

   // Move left to right, filling in the diagonal wiggles
   i = IMAX - 1;
   int istep = -1;                                               // starting position and increment of i
   for ( int j = 0; j < JMAX; j++ )
   {
      screen[i][j] = FOREGROUND;
      i += istep;                                                // next position of diagonal
      if ( i == 0 || i == IMAX - 1 ) istep = -istep;             // change direction at top or bottom
   }

   // Draw it
   for ( i = 0; i < IMAX; i++ )
   {
      for ( j = 0; j < JMAX; j++ ) cout << screen[i][j];
      cout << endl;
   }
}


##*###*###*###*##
#*#*#*#*#*#*#*#*#
*###*###*###*###*



If you change IMAX to 5, and BACKGROUND to ' ' (space), then you get the slightly more obvious
    *       *    
   * *     * *   
  *   *   *   *  
 *     * *     * 
*       *       *



Here's one requiring a lot less memory
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
#include <iostream>
#include <cmath>
using namespace std;

const int  IMAX       = 3  ;      // down
const int  JMAX       = 17 ;      // across
const char BACKGROUND = '#';
const char FOREGROUND = '*';


int ival( int j ) { return abs( IMAX - 1 - j % ( 2*(IMAX-1) ) ); }


int main()
{
   for ( int i = 0; i < IMAX; i++ )
   {
      for ( int j = 0; j < JMAX; j++ )
      {
         if ( i == ival( j ) ) cout << FOREGROUND;
         else                  cout << BACKGROUND;
      }
      cout << endl;
   }
}


##*###*###*###*##
#*#*#*#*#*#*#*#*#
*###*###*###*###*
closed account (48T7M4Gy)
@OP Your coordinated approach is very simple, congratulations :)
Extend it as your requirement dicatates, perhaps by developing the pattern using 3 <string>'s repeating every 3rd or 4th column.

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
#include <iostream>

using namespace std;

int main()
{
    
    for(int x=1; x<4; x++)
    {
        for(int y=1; y<10; y++)
        {
            if(
               (x==1 && y==3)||(x==1 && y==7)|| // LINE 1
               (x==2 && y==2)||(x==2 && y==4)||(x==2 && y==6)||(x==2 && y==8)|| // LINE 2
               (x==3 && y==1)||(x==3 && y==5)||(x==3 && y==9) // LINE 3
               )
            {
                cout<<'*';
            }
            else
                cout<<'#';
        }
        cout<<endl;
    }
}
##*###*##
#*#*#*#*#
*###*###*
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

string compose(string , int);

int main()
{
    
    for(int x=1; x<4; x++)
    {
        for(int y=1; y<10; y++)
        {
            if(
               (x==1 && y==3)||(x==1 && y==7)|| // LINE 1
               (x==2 && y==2)||(x==2 && y==4)||(x==2 && y==6)||(x==2 && y==8)|| // LINE 2
               (x==3 && y==1)||(x==3 && y==5)||(x==3 && y==9) // LINE 3
               )
            {
                cout<<"*";
            }
            else
                cout<<"#";
        }
        cout<<endl;
    }
    string line01 = "  * ";
    string line02 = " * *";
    string line03 = "*   ";
    
    int how_many = 15;
    string printout = compose(line01, how_many) + compose (line02, how_many) + compose(line03,how_many);
    
    std::cout << "\nPattern extended just for fun\n";
    std::cout << printout << '\n';
    
    return 0;
}

string compose(string aLine, int no_of_times)
{
    string result;
    for (int i = 0; i < no_of_times; i++)
        result += aLine;
    result += '\n';
    
    return result;
}



##*###*##
#*#*#*#*#
*###*###*

Pattern extended just for fun
  *   *   *   *   *   *   *   *   *   *   *   *   *   *   * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   

Program ended with exit code: 0
thank you all, you helped me quite alot here!
Topic archived. No new replies allowed.