|
-
October 25th, 2016, 06:02 PM
#1
checking 2d arrays for identical lines
Hi,
I'm writing a function which checks whether a square matrix contains
any line (horizontal, vertical, or diagonal) whose elements are
identical and nonzero (e.g., [5 5 5]). The issue is that the function
also ends up changing the matrix. It's very strange, and I'm not sure
why it's happening. Below is the source code (a MWE):
Code:
package test;
import java.util.Arrays;
public class Test {
/**
* @param args the command line arguments
*/
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 why that is, or an appropriate workaround (a
replacement for the for loops). Any help would be greatly appreciated.
Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|