# Thread: Checking 2D array for identical for identical lines

1. Member
Join Date
Oct 2016
Posts
1
Rep Power
0

## Checking 2D array for identical for identical lines

Hi,

I'm writing a simple program which checks whether a square integer matrix contains any line (horizontal, vertical, or diagonal) with identical, nonzero elements. For example [1 7 0; 0 7 3; 2 7 8] and [5 3 1; 2 5 1; 5 2 5] pass the test (second column of 7s, and diagonal of 5s, respectively), whereas [3 1 5; 3 7 2; 5 2 3] doesn't, and neither does [0 0 0; 4 1 3; 5 2 1] (since the line must be nonzero). The strange thing is that the function which checks the matrix for such ends up changing the matrix. I cannot figure out why this is happening. Below is the source code; any help would be appreciated.

Java Code:
```package test;

import java.util.Arrays;

public class Test {

public static void main(String[] args) {
// initialize a 3x3 matrix of zeros
int[][] mat;
mat = new int[3][3];

// set a single element equal to one
mat[2][0] = 1;

// print matrix
System.out.println("before checking matrix:");
printMatrix(mat);

// check matrix for a nonzero line with identical elements
checkMatrixForNonzeroLineWithIdenticalElements(mat);

// print matrix after checking
System.out.println("after checking matrix:");
printMatrix(mat);
}

// method to check square matrix for nonzero line with identical elements.
// The function checks along rows, columns, and diagonals
// Returns boolean 'true' (lines found) or 'false' (no lines found)
public static boolean checkMatrixForNonzeroLineWithIdenticalElements(int[][] mat) {
// variable for size of matrix
int n;
n = mat.length;

// variable for lines
int[] line = new int[n];

// check the Northwest-Southeast diagonal
for(int i = 0; i < n; i++) {
line[i] = mat[i][i];
if(checkLineForNonzeroIdenticalElements(line))
return true;
}

// check the Northeast-Southwest diagonal
for(int i = 0, j = n-1; i < n; i++, j--) {
line[i] = mat[i][j];
if(checkLineForNonzeroIdenticalElements(line))
return true;
}

// check the rows
for(int i = 0; i < n; i++) {
line = mat[i];
if(checkLineForNonzeroIdenticalElements(line))
return true;
}

// check the columns
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
line[j] = mat[i][j];
if(checkLineForNonzeroIdenticalElements(line))
return true;
}

// If we arrived here, we didn't find any identical nonzero lines
return false;
}

// method to check any particular integer array for nonzero, identical elements
// Returns boolean 'true' if array meets condition, otherwise 'false'.
public static boolean checkLineForNonzeroIdenticalElements(int[] array) {
if(array[0] == 0)
return false;
for(int i = 1; i < array.length; i++) {
if(array[i] != array[0])
return false;
}
return true;
}

// Method to print matrix
public static void printMatrix(int[][] mat) {
for(int i = 0; i < mat.length; i++) {
System.out.println(Arrays.toString(mat[i]));
}
}

}```
I noticed that deleting some of the for loops in the 'checkMatrixForNonzeroLineWithIdenticalElements' function fixes the issue, but I'm not sure of why this is, and what would be a workaround (a good replacement) for those for loops.

Thanks!

2. ## Re: Checking 2D array for identical for identical lines

Two things: the loop starting at line #39 is incorrect: you're checking the partially filled line array over and over again; and the loop starting at line #53 is dangerous: you assign an entire row to the line array without copying the content of the matrix row, so further loops will alter a row of the original matrix.

kind regards,

Jos

3. ## Re: Checking 2D array for identical for identical lines

Also posted at: http://forums.codeguru.com/showthrea...dentical-lines

Assigning array variables does not copy the data. It gives a second reference to the same array. An example:
Java Code:
```      int[][] twoDim = {{1,1,1}, {2,2,2}};
int[] oneDim = twoDim[0];   // refers to first row of twoDim
System.out.println("2D="+java.util.Arrays.deepToString(twoDim));
System.out.println("1D="+java.util.Arrays.toString(oneDim));
//  Change oneDim (AND twoDim)
oneDim[0] = 99;        // change a value in both
System.out.println("2D="+java.util.Arrays.deepToString(twoDim));  // 2D=[[99, 1, 1], [2, 2, 2]]  <<< Changed
System.out.println("1D="+java.util.Arrays.toString(oneDim));      // 1D=[99, 1, 1]
// copy new array
oneDim = twoDim[1].clone();   // make new copy of second row
oneDim[0] = 88;    // change a value
System.out.println("2D="+java.util.Arrays.deepToString(twoDim));  // 2D=[[99, 1, 1], [2, 2, 2]]   <<< NOT changed
System.out.println("1D="+java.util.Arrays.toString(oneDim));      // 1D=[88, 2, 2]```
Last edited by Norm; 10-26-2016 at 03:05 PM.