-2

I am working with 2-D arrays and I require help on this topic. My task is to create a 2-D array such that it is n by n (i.e. the number of rows and columns are equal). Fill the array with alternating 0's and 1's

void setup()
{ 
    int n=3;
    // code to populate the array

    // code to display the output of array in a table format

    /* output should be as follows:
    The expected result when n=3 should be as the following:

    1 0 1
    0 1 0
    1 0 1

    */
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

5 Answers5

0

Solution:

 class test{
    public static void main(String[] args) {

        int n = 3;
        int[][] arr = setup(n);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
               System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
    static int[][] setup(int n){
        int[][] arr = new int [n][n];
        boolean even = false;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
               arr[i][j] = even ? 1 : 0;
               even = !even;
            }
        }
        return arr;
    }
}

Output:

0 1 0
1 0 1
0 1 0
saintlyzero
  • 1,632
  • 2
  • 18
  • 26
0

This should work.

You can replace the 3 that is given to n with any number you want as the 2D-Array's length.

void setup(){
    int n = 3;
    int count = 0;

    int[][] myArray = new int[n][n];

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(count % 2 == 0){
                myArray[i][j] = 1;
            }
            else{
                myArray[i][j] = 0; 
            }
            System.out.print(myArray[i][j] + " ");
            count++;
        }
        System.out.println();
    }

}

Output:

0 1 0
1 0 1
0 1 0
THess
  • 1,003
  • 1
  • 13
  • 21
0

A simple implementation:

    void setup()
{
    int n=3;
    final int[][] array = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ((n * i + j) % 2 == 0) {
                array[i][j] = 1;
            }
        }
    }
}
Enrico Giurin
  • 2,183
  • 32
  • 30
0

It's not complex, you just have to iterate two loops, that's it. Although you can get the solution on different ways.

public static void main(String[] args) {
        int n = 3;
        int [][] arr = new int[n][n];
        int val = 1;
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                arr[i][j] = val;
                val = (val == 0) ? 1 : 0;
            }
        }

        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

Output:

0 1 0
1 0 1
0 1 0
Amit
  • 1,540
  • 1
  • 15
  • 28
0

Here is how you can approach your logic , create a counter and negate each time after it prints value.

 public static void main(String[] args) {
    int counter = 0;
    int n = 3;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            System.out.print(counter==0?1:0);
            counter = ~counter;
        }
        System.out.println();
    }

}

Output

 101
 010
 101

Here is Online source code.

manikant gautam
  • 3,521
  • 1
  • 17
  • 27