This is a school problem I am working on for an intro Java class. The assignment is to write a program that generates an 8 x 8 matrix of randomly generated binary numbers and have the program check which, if any, columns are all 0's and if the major and minor diagonals are also comprised of zeroes. A major diagonal is the diagonal formed from the top left corner to the bottom right corner (ie arrayName[0][0] to arrayName[8][8] in this case) and a minor diagonal is one that goes from the top right corner to the bottom left corner of the matrix.
I've gotten everything working except for the part that checks for the major and minor diagonals, and I can't figure out why this isn't working. What I've been trying to do is just count the number of zeros along the diagonal, and if that number is 8, then you've got yourself a diagonal comprised of 0's. Here are the two methods that I have for the major and minor array:
public static void majorDiagonal(int[][] board)
{
byte count = 0;
for(int row = board.length - 1, offsetNumber = board.length - 1; row > 0; row--, offsetNumber--)
for(int column = board.length - 1; column > 0; column--)
if(board[row][offsetNumber] == 0) count++;
if(count == 8) System.out.println("All zeroes on the major diagonal");
}
public static void minorDiagonal(int[][] board)
{
byte count = 0;
for(int row = board.length - 1, offsetNumber = 0; row > 0; row--, offsetNumber++)
for(int column = board.length - 1; column > 0; column--)
if(board[row][offsetNumber] == 0) count++;
if(count == 8) System.out.println("All zeroes on the minor diagonal");
}
An interesting error that I encountered when I tried to find the major diagonal by counting up in the for loop, ie:
for(int row = 0; row< board.length; row++) for(int column = 0; column < board.length; column++) if(board[row][row] == 0) count++;The code wouldn't work, but if the diagonal had all 1's and a single 0, it would print "All zeros on the major diagonal" even though the variable, count, wasn't eight.
Hope this makes sense, thanks for any help.