Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2016
    Posts
    1
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 04-18-2013, 09:48 AM
  2. identical arrays program
    By grcarr77 in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 10:05 PM
  3. Two identical strings are not identical...
    By murphaph in forum New To Java
    Replies: 7
    Last Post: 01-05-2010, 09:58 AM
  4. comparison of two (identical) strings doesn't work
    By langaro in forum CLDC and MIDP
    Replies: 4
    Last Post: 10-02-2009, 04:02 PM

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
  •