printing a unique 2D array with alternate 1's and 0's

I want to print 2D array which is like this..
If size is 5*5 then it output should be:
00000
01110
01010
01110
00000



My code is this:



#include <iostream>
#include<Windows.h>
using namespace std;


int main()
{ const int r=10;//rows
const int c=10;//columns
int a[r][c];//declaration of 2d array
int f=r/2+1;//This is to limit the number of circles in the printing

for(int circle=0;circle<f;circle++)
{ for(int i=0+circle;i<r-circle;i++)
{ cout<<endl;
for(int j=0+circle;j<c-circle;j++)
cout<<(circle%2);


}



}
Last edited on
If you just need binary digits, maybe you should use a bool, not an int..
It will do you a lot of help to use a consistent indentation style as well. Also, I do not know why you are #including <Windows.h> -- no such file exists and even if it did you don't need it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
  const int r=10;
  ...

  for (...)
  {
    cout << "Hello world!\n";
  }

  for (...)
  {
    
  }

  return 0;
}

I agree with strongdrink -- you are approaching this the hard way. In order to create a number like '01110' you need to play with individual digits anyway, so why mess around with trying to pack them into a single number? Just print five individual numbers: 0, 1, 1, 1, and 0, which will appear as
01110
You don't necessarily need to use bool, but whatever type you use you only will use two values, 0 and 1.

As you are working on a circle, here are some hints to help:

1. Fill your array with '0' first, before doing anything else.
2. Calculate where the individual '1's go in the array, using your circle algorithm.
3. Print the array, which will require only two for loops (one nested in the other).

There are a variety of ways to calculate a circle. Google around "circle algorithm" for help there. At the very worst you can use some simple trigonometry (#include <cmath>).

Good luck!
Topic archived. No new replies allowed.