How to print following pattern in C++ :1st row->10101010...;2nd row->11001100..;3rd row ->11100011 and so on.
Number of alternate 0 and 1s depends on the row number.
Pattern should look like:
1010101010
1100110011
1110001110
Code:
#include<iostream>
using namespace std;
int main()
{
int row,col,cond;
cout <<"Enter the number of rows: ";
cin >> row;
cout <<"Enter the number of cols: ";
cin >> col;
int a[row][col]={0};
for(int i=0; i<row; i++)
{
for(int j=0;j<col;j++)
{
cond = (((j+1)/(i+1))+i+1);
// cout << "i=" << i <<"j="<<j<<"cond="<<cond <<endl;
if( (j <= i) )
{
a[i][j]=1;
if(cond+j<col)
{
a[i][cond+j]=1;
}
}
else if (a[i][j] !=1)
{
a[i][j]=0;
}
}
}
for(int i=0; i<row; i++)
{
for(int j=0;j<col;j++)
{
cout << a[i][j];
}
cout<<endl;
}
return 0;
}
Ouput:
Enter the number of rows: 4
Enter the number of cols: 9
101000000
111010000
111110100
111111101