1

Trying to make an n*n pattern of "+" and "-" when value a[i][j] == b[i][j] and value a[i][j] != b[i][j]. Unsure of why this isn't outputting expected.

Same output if I loop through a[i][j] == seq[i]. Tried assigning a[i][j] and seq[i] to variables before comparing and same result.

public class ThueMorse {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        String thue   = "0";
        String morse  = "1";
        String[][] a = new String[n][n];
        String[][] b = new String[n][n];
    

        for (int i = 1; i <= n; i++) {
            String t = thue;
            String m = morse;
            thue  += m;
            morse += t;
            if (thue.length() >= n) break;
        }
        String [] seq = new String[n];
        seq = thue.split("");
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                a[i][j] = seq[j];
                b[i][j] = seq[i];
                
                if (j != 0) System.out.print(" ");
                if (a[i][j] == b[i][j]) {
                System.out.print("+");
                }
                else {
                System.out.print("-");
                }

                if (j != n) System.out.print(" ");
            }    
            System.out.println();
        }
    }
}

For reference:

seq[] = [0, 1, 1, 0, 1, 0, 0, 1]

a[i][j] =

0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1

b[i][j] =

0  0  0  0  0  0  0  0
1  1  1  1  1  1  1  1
1  1  1  1  1  1  1  1
0  0  0  0  0  0  0  0
1  1  1  1  1  1  1  1
0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0
1  1  1  1  1  1  1  1

Output:

+  -  -  -  -  -  -  -
-  +  -  -  -  -  -  -
-  -  +  -  -  -  -  -
-  -  -  +  -  -  -  -
-  -  -  -  +  -  -  -
-  -  -  -  -  +  -  -
-  -  -  -  -  -  +  -
-  -  -  -  -  -  -  +

Expected:

+  -  -  +  -  +  +  -
-  +  +  -  +  -  -  +
-  +  +  -  +  -  -  +
+  -  -  +  -  +  +  -
-  +  +  -  +  -  -  +
+  -  -  +  -  +  +  -
+  -  -  +  -  +  +  -
-  +  +  -  +  -  -  +
Lachie
  • 21
  • 3
  • 1
    you are aware that in Java `String`s are NOT compared using `==`, but with `equals()` ?! You can see that it works as wanted on https://ideone.com/EzmkG8 – user16320675 Apr 19 '23 at 09:27
  • 1
    see [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?r=SearchResults&s=3%7C166.7309) – user16320675 Apr 19 '23 at 09:33

2 Answers2

3

The issue with the code seems to be in the initialization of the b array.

In the nested for loop where you are assigning values to b, you are using seq[j] instead of seq[i] as the value for each element.

To fix this, change the line

b[j][i] = seq[j];

to

b[i][j] = seq[j];

Once you make this change, the code should produce the expected output.

Ertersy
  • 41
  • 5
  • I changed b[ j ] [ i ] = seq [ j ] to b [ i ] [ j ] = seq [ i ] and got the same output. As b [ i ] [ j ] = seq [ j ] would be the same array as a[ ]. – Lachie Apr 19 '23 at 08:11
1

Turns out .split() was causing the issue. Converting the values from seq into a new array of type int fixed the problem.

    String [] seq = new String[n];
    seq = thue.split("");
    
    int[] num = new int[n]; 
    
    for (int i = 0; i < n; i++) {
        num[i] = Integer.parseInt(seq[i]);
    }
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++) {
            a[i][j] = num[j];
            b[i][j] = num[i];
            if (j != 0) System.out.print(" ");
            if (a[i][j] == b[i][j]) {
                System.out.print("+");
            }
            else {
                System.out.print("-");
            }

            if (j != n) System.out.print(" ");
        }
        System.out.println();
    }
Lachie
  • 21
  • 3