-
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!
-
October 26th, 2016, 03:06 AM
#2
Re: checking 2d arrays for identical lines
Shouldn't this code
Code:
// check the Northwest-Southeast diagonal
for(int i = 0; i < n; i++) {
line[i] = mat[i][i];
if(checkLineForNonzeroIdenticalElements(line))
return true;
}
be
Code:
// check the Northwest-Southeast diagonal
for(int i = 0; i < n; i++)
line[i] = mat[i][i];
if(checkLineForNonzeroIdenticalElements(line))
return true;
so that line is filled before the single call to the check..() function?
Same for the other diagonal?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 26th, 2016, 07:50 AM
#3
Re: checking 2d arrays for identical lines
Norm
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
|