/* * This program tests if a square double array has any row or column * full of zeroes * @author Jose V Nunez * license: GPL * Blog: El Angel Negro - http://elangelnegro */ public class SquareMatrix { /** * Command line test * @param args */ public static void main(String [] args) { int [][] hasZeros = { {0, 8, 0, 3, 0}, {0, 1, 2, 0, 0}, {0, 0, 3, 0, 2}, {1, 5, 0, 6, 0}, {0, 0, 0, 0, 0} }; int [][] noZeros = { {0, 8, 0, 3, 0}, {0, 1, 2, 0, 0}, {4, 0, 3, 0, 2}, {0, 5, 0, 6, 0}, {0, 0, 1, 0, 0} }; if (hasZero(hasZeros)) { System.out.println("Has zeros"); } if (! hasZero(noZeros)) { System.out.println("Has no zeros"); } } /** * Detect if a square array has a file or row full of zeroes * @param a The double array to check * @return boolean */ public static boolean hasZero(int [][] a) { boolean status = false; int rowLength = a.length; int colLength = rowLength; for (int i = 0; i < rowLength; i++) { int colCount = 0; int rowCount = 0; for (int j = 0; j < colLength; j++) { colCount += a[i][j]; rowCount += a[j][i]; } // Count at the end of the row if (colCount == 0 || rowCount == 0) { status = true; return status; } } return status; } }