views:

389

answers:

2

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.

A: 

Your diagonal elements are in board[0][0], board[1][1], board[2][2], so there's no need for two loops. It's just board[i][i].

public static void majorDiagonal(int[][] board)
{
    byte count = 0;
    for (int i = 0; i < board.length; ++i)
    {
        if (board[i][i] == 0) count++;
    }
    if(count == board.length) System.out.println("All zeroes on the major diagonal");
}

And the minor is similar. board[0][7], board[1][6], board[2][5], which we can simplify to board[i][board.length-1-i].

public static void minorDiagonal(int[][] board)
{
    byte count = 0;
    for (int i = 0; i < board.length; ++i)
    {
        if (board[i][board.length-1-i] == 0) count++;
    }
    if(count == board.length) System.out.println("All zeroes on the minor diagonal");
}
Nick Lewis
All good, though it's probably worth using `length` rather than literal `8` in the final check (since you use it in the loop), to make this slightly more generic.
Pavel Minaev
Good point, I just copy-pasted the print line from the original question.
Nick Lewis
Thank you for the specifics. I owe you guys one.
maxvcore
+6  A: 

You're overcomplicating things. Consider for a moment the positions of elements that you need to check. For a major diagonal, the indices are: (0,0), (1,1), (2,2), ..., (7,7). Now pause, observe the pattern of changes, and think: how many loops do you actually need to generate such a sequence? Minor diagonal is only superficially different: (0,7), (1,6), (2,5), ..., (7,0) - which can be rewritten as: (7-7, 7), (7-6, 6), (7-5, 5), ... (7-0, 0). Again, consider how many loops you actually need.

Pavel Minaev
Spot on: Unless the requirement is to use two separate functions, you should combine all this into a single loop.
DisgruntledGoat
Thank you, I guess I just got stuck thinking in a robotic fashion.
maxvcore