-1

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
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • What have you tried so far? – gerum Mar 22 '22 at 08:27
  • @WeatherVane language preferred is CPP or C as mentioned in question – Jyoti Haveliya Mar 22 '22 at 08:34
  • use `std::string`. The constructor that takes a `char` and number of repetitions comes in very handy here, eg `std::string x(3,'1');` – 463035818_is_not_an_ai Mar 22 '22 at 08:47
  • Run your code with an address sanitizer. If you use GCC you could use options `-fsanitize=address -fsanitize=undefined`. Your inner loop's condition `j<=col` creates an array index out of range for `j=col`, and even with `j < col`, `a[i][cond+j]=1;` will create an out-of-bounds access if `cond != 0`. Hint: When the column index (starting with 0) is divisable by the row number (starting with 1), you have to switch between `0` and `1`. The task could be solved by using a modulo operator like `j % (i+1)` or by using an additional counter that counts the number of subsequent `1` or `0` characters. – Bodo Mar 22 '22 at 08:48
  • 1
    You should [edit] your question and explain what exactly means "is not working". Show the error message(s) or wrong output. – Bodo Mar 22 '22 at 08:51
  • `int a[row][col]={0};` is not standard C++. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Mar 22 '22 at 09:01
  • @Bodo I have edited as per suggestion. – Jyoti Haveliya Mar 22 '22 at 09:17

2 Answers2

1

You are overcomplicating this - you don't need any arrays or tricky indexing.
Print one row at a time, starting with 1.
Switch back and forth between 0 and 1 at the appropriate columns.

for (int r = 1; r <= row; r++)
{
    int symbol = 1;
    for (int c = 1; c <= col; c++)
    {
        std::cout << symbol;
        if (c % r == 0)
        {
            symbol = 1 - symbol;
        }
    }
    std::cout << std::endl;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

if you can use string ,you can create that and then slice it .

#include <iostream>
#include <string>
using namespace std;
std::string createStringChars(const char theChar, int repeatNo)
{
    return std::string(repeatNo, theChar); //NRVO caller
}
std::string createAlignedString(int rows, int cols)
{
    std::string result;

    {
        std::string temp;
        for(int i = 0; i < rows; ++i)
        {
            temp.clear();
            bool nextChar = false;
            for(int j = 0; j < (cols / (i + 1) + 1); ++j)
            {
                if(nextChar)
                    temp += createStringChars('0', i + 1);
                else
                    temp += createStringChars('1', i + 1);
                nextChar = !nextChar;
            }
            result += (temp.substr(0, cols) + "\n");
        }
    }
    return result;
}

int main()
{
    int row = 3, col = 10;
    cout << "Enter the number of rows: ";
    cin >> row;
    cout << "Enter the number of cols: ";
    cin >> col;
    cout << "\n@@@@ result @@@@ \n" << createAlignedString(row, col) << endl;
    return 0;
}

H.M
  • 425
  • 2
  • 16