Ok, I have three main tasks.
One: I need to create a 2 dimensional array that is 8x8 and consists of randomly generated numbers that range from 0-3. I'm assuming the Math.random will be used inside of a for loop with a limit set for length(8) and range(0-3) somehow?

Two: This part is multi-pronged but the main thing I need to figure out is how to analyze the array once created. I need to print out if it finds at least 3 consecutive numbers in any row, column, diagonal, or subdiagonal(under a diagonal?). I would use a search but my understanding is that the search inside of an array using a for loop dies once it finds one number. so if the array is 01233323 it will still come up as having found nothing because it will stop on the first 3 ( or it will stop on the first number considering that it will be searching for all of the numbers I have generated).

Third: I need to figure out/learn the solution for this problem. This is homework. I would love to be able to ask you to write my code for me but that would get me nowhere and it would waste your time.

I really want to be able to post the code I have so far but its basically the first lines netbeans generates. I have been all over the page, rewatched my lectures(its recorded), and checked the book. From what I can tell there is no clear solution. Any help would be greatly appreciated.

Thanks again,
24x24

Ok I have my matrix (google explained that! too bad I didn't figure it out before I posted) I now only need to figure out the second part of my task. Analyzing the matrix. This is really the portion of the project that has been hard for me to wrap my head around. Here is what I have so far:

package project4;

import java.util.Random;

/**
 *
 * @author 24x24
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Create matrix

        final int ROW_WIDTH = 8;
        final int COL_HEIGHT = 8;
        Random random = new Random();
        int[][] matrix = new int[ROW_WIDTH][COL_HEIGHT];
        //fill the matrix

        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++) {
                matrix[row][col] = random.nextInt(4);
            }
        }
        //Print matrix

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
             
            }
            System.out.println();
        }
    }
}

Sorry its got very little comments. Just got it written and wanted help for what I didn't have help for instead of you guys wasting time explaining what I have done so far.

If you are using matrix.length, then you don't need line 17 and 18. Actually, you already know that the 2D array (you called matrix) has length 8x8, so only one constant variable would be enough. Unless you don't want to use the constant variable but keep using matrix.length, then you don't need that constant variable at all.

Second part is to design how you would go through the whole 2D array and keep checking for value. What you can do is to create 3 types of mask you want to look for. Each mask has starting location and length of 3. They all have starting location at the top. First one goes downward, second one goes sideward, and the last one goes diagonal from top-left to bottom-right.

/*
   1          1 2 3        1
   2                        2
   3                         3
 first        second      third
 */

Then use this mask to check through your 2D array. That's it! Done... Hmm... Not sure how much knowledge in programming you know though...

I understand the theory, its the code portion I am having an issue with. For what you stated would it be possible to do something like this:

if (matrix[i][j]==0 && matrix[i][j+1]==0 && matrix[i][j+2]==0){
k++}
else
i++

If this would work, how would I make it go through the whole row? Can I put j++ inside of a for loop and have it move all the way across until it reaches the length?
Also what if it comes across 4 0's? The program instructions say "At least 3 consecutive numbers in any row".

Thanks for any help

Ok this is what I have:

// return matrix;
        int k = 0;
        int row1 = 0;
         int row2 = 0;
          int row3 = 0;
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix.length; c++) {
                if (matrix[r][c] == 0 && matrix[r][c + 1] == 0 && matrix[r][c + 2] == 0) {
                    k++;
                } else {
                    r++;

                }
                row1++;
            }//end of column for loop
            row2++;
        }//end of row for loop
          row3++;
        System.out.println("Got at least " + k + " consecutive 0's in row1 " + row1 + "row2" + row2 + "row3" + row3 );
    }//end of create method

I stepped through the debugger and from what I can tell, it is working. The downside to it is that it only goes to the third number. So I obviously need to find a way of making it go to the end of the line.

The row1, row2, row3 thing is just there because I need to print out what row it found matches in and I was testing to see where it would increment the row to print out the number. I haven't found any matches yet so I left it in.

I added a -1 to the matrix.length in each for loop so it wouldn't go out of bounds

With the following code:

int k = 0;
        int row = 0;
         
        for (int r = 0; r < matrix.length-1; r++) {
            for (int c = 0; c < matrix.length-1; c++) {
                if (matrix[r][c] == 0 && matrix[r][c ++] == 0 && matrix[r][(c++) + 1] == 0) {
                    k++;
                } else {
                    r++;

                }
                row++;
                System.out.println("Got at least " + k + " consecutive 0's in row " + row);
            }//end of column for loop
           
        }//end of row for loop

I get the output of:

run:
0 1 0 0 1 1 1 2
2 2 0 2 2 0 3 0
1 3 0 1 2 3 2 0
2 1 3 1 1 0 2 1
0 0 2 2 0 3 0 2
3 2 0 1 0 1 3 1
2 3 2 3 2 3 1 1
3 3 3 1 2 3 3 3
Got at least 1 consecutive 0's in row 1
Got at least 1 consecutive 0's in row 2
Got at least 1 consecutive 0's in row 3
Got at least 1 consecutive 0's in row 4
Got at least 1 consecutive 0's in row 5
Got at least 1 consecutive 0's in row 6
BUILD SUCCESSFUL (total time: 0 seconds)

which obviously has no consecutive 0's and it stopped at row 6 or I get:

run:
0 2 3 2 3 0 0 1
3 1 3 2 1 2 0 1
0 1 2 2 3 3 0 0
0 3 0 1 0 2 2 1
3 2 3 2 2 2 3 1
0 0 3 2 3 2 1 3
1 0 3 0 3 3 0 3
1 0 1 0 1 1 0 2
Got at least 0 consecutive 0's in row 1
Got at least 0 consecutive 0's in row 2
Got at least 0 consecutive 0's in row 3
Got at least 0 consecutive 0's in row 4
Got at least 0 consecutive 0's in row 5
Got at least 0 consecutive 0's in row 6
Got at least 1 consecutive 0's in row 7
Got at least 1 consecutive 0's in row 8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at project4.Main.main(Main.java:58)
Java Result: 1

Again, none are consecutive. It went all the way through row 8 the second run (This seems to be random) but it threw the error.

Any suggestions?

Also I am aware I will need to use an if statement for the counter, but currently it isn't finding the proper values so that portion doesn't matter anyway

By the way, there should be 4 types of mask, not 3 types. My bad.

No no... The way you go through the array is ...

for (int row=0; row<matrix.length; row++) {
  for (int col=0; col<matrix.length; col++) {
    // for all masks, check the first and last position whether they are in range
    // if they are ALL in range, then you can further check if the value
    // inside are consecutive
  }
}

/*
 for example...

 + - + - + - + - + - + - + - + - +
 | 0 | 1 | 0 | 2 | 3 | 3 | 2 | 0 |
 + - + - + - + - + - + - + - + - +
 | 1 | 1 | 3 | 3 | 3 | 3 | 0 | 1 |
 + - + - + - + - + - + - + - + - +
 | 0 | 1 | 0 | 2 | 3 | 1 | 2 | 1 |
 + - + - + - + - + - + - + - + - +
 | 3 | 1 | 0 | 2 | 3 | 3 | 2 | 3 |
 + - + - + - + - + - + - + - + - +
 | 0 | 3 | 0 | 2 | 3 | 3 | 2 | 3 |
 + - + - + - + - + - + - + - + - +
 | 0 | 2 | 3 | 2 | 0 | 3 | 3 | 2 |
 + - + - + - + - + - + - + - + - +
 | 2 | 1 | 0 | 2 | 3 | 3 | 3 | 1 |
 + - + - + - + - + - + - + - + - +
 | 0 | 1 | 1 | 2 | 3 | 1 | 2 | 2 |
 + - + - + - + - + - + - + - + - +

 For mask#1, you start at 0,0 (row, column) and keep going through column until
 the end because it is vertical check. You need to keep checking if the end
 position is a valid location. If it is not, you skip this check.

 for mask#2, while you are going through the same loop,  keep going through column
 until the mask becomes invalid at position 0,7 because it is horizontal check.
 Then you skip this check until the next row. 

 for mask#3, still inside the same loop, you would start with 0,0 and end with 2,2
 (startRow+2, startCol+2). Remember to keep checking whether the end position is
  valid each time a loop is going through.

 for mask#4, still inside the same loop, you would not start it until the start
 location is at least 2 (at 2,2) and the end will be 0,0 (startRow-2, startCol+2).

 Iteration at row=0, col=0
 mask#1 - start 0,0 and end 2,0 => value [0, 1, 0] => false
 mask#2 - start 0,0 and end 0,2 => value [0, 1, 0] => false
 mask#3 - start 0,0 and end 2,2 => value [0, 1, 0] => false
 mask#4 - start 0,0 and end 2,-2 => skip

 Iteration at row=0, col=1
 mask#1 - start 0,1 and end 2,1 => value [0, 3, 0] => true, count it!
 mask#2 - start 0,1 and end 0,3 => value [0, 2, 3] => false
 mask#3 - start 0,1 and end 2,3 => value [0, 3, 3] => false
 mask#4 - start 0,1 and end 2,-1 => skip

 Iteration at row=0, col=2
 mask#1 - start 0,2 and end 2,2 => value [0, 1, 0] => false
 mask#2 - start 0,2 and end 0,4 => value [0, 1, 0] => false
 mask#3 - start 0,2 and end 2,4 => value [0, 1, 0] => false
 mask#4 - start 0,2 and end 2,0 => value [0, 1, 0] => false

 ... and go on until the next row, repeat the same check ...
 */

Ok so my version of going through the array looks similar to yours

//Mine
for (int r = 0; r < matrix.length-1; r++) {
            for (int c = 0; c < matrix.length-1; c++) {
//Yours
for (int row=0; row<matrix.length; row++) {
  for (int col=0; col<matrix.length; col++) {

Except I put the -1 in to keep it from going out of bounds. I see where that is wrong. So from what I gather I need to make an if that states something like
if (r<=7 && r>=0){
continue with loop}
else
move to next iteration
And I'll need to set that for each iteration be it row, column or both?

not sure if this helps but i believe you should change k back to 0 somewhere

thats why it showed that you found one consecutive 0 in row 8

Got at least 1 consecutive 0's in row 7
Got at least 1 consecutive 0's in row 8

because k is already 1 after it found one in row 7
if there was another 000 in row 8
then the output would be got at least 2 consecutive 0's in row 8

sorry if i'm not that helpful

Hey any help is good. I saw that earlier and it seems as though I don't need to count the occurrences. So where I was using k to count how many times it happened I am just supposed to put in that it found "at least 3 consecutive 0's".

then maybe you should control it a bit so you can test if it works with the controlled environment

i dunno you can make it a smaller array or maybe add the numbers in yourself or something

Ok I kind of have the row counter working. It is finding consecutive numbers and it knows what row they are in. The problem now is that if there are 4 or more 0's it prints that it has found it twice. Is there a way to make it stop once it gets a hit? Here is the code:

int row = 0;
//r for row and c for column
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 1; c < matrix.length; c++) {
                if (r <= 7 && r >= 0 && c <= 7 && c >= 0) {//this is put into place to keep values within matrix

                    if (matrix[r][c-1] == 0 && matrix[r][c] == 0 && matrix[r][c+1] == 0) {//this is supposed to compare 3 numbers in a row
                        System.out.println("Got at least three consecutive 0's in row " + (r+1));
                    } else {
                       // r++;//moving to next row
                    }
                   
                }

            }//end of column for loop
        }//end of row for loop

Also I'm still getting random errors of out of bounds and java result:1. They only happen every other time and they don't interfere with the results so far as I can tell.

The problem was in row 7 by the way. I was checking for the number the wrong way. c-1, c and c+1 fixed it so that it found it properly. Something about incrementing it while inside of the if wasn't doing it.

Ok I was getting errors due to the index going outside of the matrix. This was only happening when it would get to reference a point in the matrix that was at 6 or 7 and then it would add to c and try to find something out of bounds. I have fixed it with this:

//r for row and c for column
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix.length-2; c++) {
                if (matrix[r][c] == 0 && matrix[r][c+1] == 0 && matrix[r][c + 2] == 0) {//this is supposed to compare 3 numbers in a row
                    System.out.println("Got at least three consecutive 0's in row " + (r + 1));
                    break;
                }
            
        }//end of column for loop for 0
    }//end of row for loop for 0

I also added a break to make it stop counting once it found a match. Now I just need to do the columns, diagonal, subdiagonal and then break it all out into methods and make it pretty. Hopefully I don't have too many issues. I'm assuming columns will be easy (just switch r and c). Diagonal is still something I will have to play with and supposedly a subdiagonal is below a diagonal? I don't really see the difference but whatever. I'll post if I need help and if not I'll mark it solved. Thanks for the help so far!

Edited 5 Years Ago by 24x24: n/a

OK, I have a finished code. Everything works and it all looks nice and pretty. I broke it all out into methods and put as many comments in as I have lines of code. They make it easier to read when I go back to figure out what I was doing but also helps the people trying to learn from what I've done. Variables are named appropriately and I think everything makes sense. This is what I have (For learning purposes only. Please write your own code. PM me if you have questions!):

/*
 * This program will create and display a 2 dimensional array with randomly generated numbers from 0 to 3.
 *  It will analyze the matrix and report if it finds at least 3 consecutive numbers in any row,
 * column, diagonal, or subdiagonal. Nothing is reported if no consecutive numbers are found.
 */
package project4;

import java.util.Random;

/**
 *
 * @author 24x24
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        //declaring variables
        final int ROW_WIDTH = 8;//final integer
        final int COL_HEIGHT = 8;//final integer

        int matrix[][] = makeMatrix(ROW_WIDTH, COL_HEIGHT);//calls the makeMatrix method
        display(matrix);//calls the display method
        findRows(matrix);//calls the findRows method
        findColumns(matrix);//calls the findColumns method
        findDiag(matrix);//calls the findDiag method
        findSub(matrix);//calls the findSub method

    }//end of main method

    /**
     * This method creates a 2 dimensional array
     * Precondition ROW_WIDTH and COL_WIDTH need to be constants
     * Postcondition matrix will be a 2 dimensional array
     * @param ROW_WIDTH
     * @param COL_HEIGHT
     * @return matrix
     */
    public static int[][] makeMatrix(int ROW_WIDTH, int COL_HEIGHT) {

        Random random = new Random();//creating random variable

        //create the matrix
        int[][] matrix = new int[ROW_WIDTH][COL_HEIGHT];

        //fill the matrix
        for (int row = 0; row < matrix.length; row++) {//changes rows
            for (int col = 0; col < matrix[row].length; col++) {//changes columns
                matrix[row][col] = random.nextInt(4);//randomize fill from 0-3
            }
        }//end of fill for loop
        return matrix;
    }//end of makeMatrix method

    /**
     * This method prints the 2 dimensional array known as the Matrix
     * Precondition the matrix must have been created
     * Postcondition the matrix must be printed
     * @param matrix
     */
    public static void display(int[][] matrix) {
        //Print matrix
        for (int r = 0; r < matrix.length; r++) {//changes rows
            for (int c = 0; c < matrix[r].length; c++) {//changes columns
                System.out.print(matrix[r][c] + " ");

            }
            System.out.println();
        }//end of matrix print

    }//end of display method

    /**
     * This method will find consecutive numbers within the rows of the matrix
     * Precondition the matrix must exist
     * postcondition the consecutive numbers in the rows will have been found
     * and the rows number containing consecutive numbers will be printed
     * @param matrix
     */
    public static void findRows(int[][] matrix) {
        //ROWS
        System.out.println("Row Analysis:");
        //Finding 0's in rows
        for (int r = 0; r < matrix.length; r++) {//changes rows
            for (int c = 0; c < matrix.length - 2; c++) {//changes columns
                if (matrix[r][c] == 0 && matrix[r][c + 1] == 0 && matrix[r][c + 2] == 0) {//this compares 3 numbers in a row
                    System.out.println("Got at least three consecutive 0's in row " + (r + 1));//prints outcome of search if it found something
                    break;//ends if it finds a match
                }
            }//end of column for loop for 0
        }//end of row for loop for 0
        //Finding 1's in rows
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix.length - 2; c++) {
                if (matrix[r][c] == 1 && matrix[r][c + 1] == 1 && matrix[r][c + 2] == 1) {
                    System.out.println("Got at least three consecutive 1's in row " + (r + 1));
                    break;
                }
            }//end of column for loop for 1
        }//end of row for loop for 1
        //Finding 2's in rows
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix.length - 2; c++) {
                if (matrix[r][c] == 2 && matrix[r][c + 1] == 2 && matrix[r][c + 2] == 2) {
                    System.out.println("Got at least three consecutive 2's in row " + (r + 1));
                    break;
                }
            }//end of column for loop for 2
        }//end of row for loop for 2
        //Finding 3's in rows
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix.length - 2; c++) {
                if (matrix[r][c] == 3 && matrix[r][c + 1] == 3 && matrix[r][c + 2] == 3) {
                    System.out.println("Got at least three consecutive 3's in row " + (r + 1));
                    break;
                }
            }//end of column for loop for 3
        }//end of row for loop for 3
    }//end of findRows method

    /**
     * This method will find consecutive numbers within the columns of the matrix
     * Precondition the matrix must exist
     * postcondition the consecutive numbers in the columns will have been found
     * and the columns number containing consecutive numbers will be printed
     * @param matrix
     */
    public static void findColumns(int[][] matrix) {
        //COLUMNS
        System.out.println("Column Analysis:");
        //Finding 0's in columns
        for (int c = 0; c < matrix.length; c++) {//changes column
            for (int r = 0; r < matrix.length - 2; r++) {//changes row
                if (matrix[r][c] == 0 && matrix[r + 1][c] == 0 && matrix[r + 2][c] == 0) {//this compares 3 numbers in a row
                    System.out.println("Got at least three consecutive 0's in column " + (c + 1));//prints outcome of search if it found something
                    break;//ends if it finds a match
                }
            }//end of column for loop for 0
        }//end of row for loop for 0
        //Finding 1's in columns
        for (int c = 0; c < matrix.length; c++) {
            for (int r = 0; r < matrix.length - 2; r++) {
                if (matrix[r][c] == 1 && matrix[r + 1][c] == 1 && matrix[r + 2][c] == 1) {
                    System.out.println("Got at least three consecutive 1's in column " + (c + 1));
                    break;
                }
            }//end of column for loop for 1
        }//end of row for loop for 1
        //Finding 2's in columns
        for (int c = 0; c < matrix.length; c++) {
            for (int r = 0; r < matrix.length - 2; r++) {
                if (matrix[r][c] == 2 && matrix[r + 1][c] == 2 && matrix[r + 2][c] == 2) {
                    System.out.println("Got at least three consecutive 2's in column " + (c + 1));
                    break;
                }
            }//end of column for loop for 2
        }//end of row for loop for 2
        //Finding 3's in columns
        for (int c = 0; c < matrix.length; c++) {
            for (int r = 0; r < matrix.length - 2; r++) {
                if (matrix[r][c] == 3 && matrix[r + 1][c] == 3 && matrix[r + 2][c] == 3) {
                    System.out.println("Got at least three consecutive 3's in column " + (c + 1));
                    break;
                }
            }//end of column for loop for 3
        }//end of row for loop for 3
    }//end of findColumns method

    /**
     * This method will find consecutive numbers within the diagonal of the matrix
     * Precondition the matrix must exist
     * postcondition the consecutive numbers in the diagonal will have been found
     * and the diagonal number containing consecutive numbers will be printed
     * @param matrix
     */
    public static void findDiag(int[][] matrix) {
        //DIAGONAL
        System.out.println("Diagonal Analysis:");
        //Finding 0's in diagonal
        for (int d = 0; d < 5; d++) {//going from top left to bottom right
            if (matrix[d][d] == 0 && matrix[d + 1][d + 1] == 0 && matrix[d + 2][d + 2] == 0) {//this compares 3 numbers in a diagonal
                System.out.println("Got at least three consecutive 0's in diagonal ");//prints outcome of search if it found something
                break;//ends if it finds a match
            }
        }//end of diagonal for loop for 0
        //Finding 1's in diagonal
        for (int d = 0; d < 5; d++) {
            if (matrix[d][d] == 1 && matrix[d + 1][d + 1] == 1 && matrix[d + 2][d + 2] == 1) {
                System.out.println("Got at least three consecutive 1's in diagonal ");
                break;
            }
        }//end of diagonal for loop for 1
        //Finding 2's in diagonal
        for (int d = 0; d < 5; d++) {
            if (matrix[d][d] == 2 && matrix[d + 1][d + 1] == 2 && matrix[d + 2][d + 2] == 2) {
                System.out.println("Got at least three consecutive 2's in diagonal ");
                break;
            }
        }//end of diagonal for loop for 2
        //Finding 3's in diagonal
        for (int d = 0; d < 5; d++) {
            if (matrix[d][d] == 3 && matrix[d + 1][d + 1] == 3 && matrix[d + 2][d + 2] == 3) {
                System.out.println("Got at least three consecutive 3's in diagonal ");
                break;
            }
        }//end of diagonal for loop for 3
    }//end of findDiag method

    /**
     * This method will find consecutive numbers within the subdiagonal of the matrix
     * Precondition the matrix must exist
     * postcondition the consecutive numbers in the subdiagonal will have been found
     * and the subdiagonal number containing consecutive numbers will be printed
     * @param matrix
     */
    public static void findSub(int[][] matrix) {
        //SUBDIAGONAL
        System.out.println("Sub-Diagonal Analysis:");
        //Finding 0's in subdiagonal
        for (int r = 0, c = 7; c > 2; r++, c--) {//going from bottom right to top left
            if (matrix[r][c] == 0 && matrix[r + 1][c - 1] == 0 && matrix[r + 2][c - 2] == 0) {//this compares 3 numbers in a diagonal
                System.out.println("Got at least three consecutive 0's in subdiagonal ");//prints outcome of search if it found something
                break;//ends if it finds a match
            }
        }//end of subdiagonal for loop for 0
        //Finding 1's in subdiagonal
        for (int r = 0, c = 7; c > 2; r++, c--) {
            if (matrix[r][c] == 1 && matrix[r + 1][c - 1] == 1 && matrix[r + 2][c - 2] == 1) {
                System.out.println("Got at least three consecutive 1's in subdiagonal ");
                break;
            }
        }//end of subdiagonal for loop for 1
        //Finding 2's in subdiagonal
        for (int r = 0, c = 7; c > 2; r++, c--) {
            if (matrix[r][c] == 2 && matrix[r + 1][c - 1] == 2 && matrix[r + 2][c - 2] == 2) {
                System.out.println("Got at least three consecutive 2's in subdiagonal ");
                break;
            }
        }//end of subdiagonal for loop for 2
        //Finding 3's in subdiagonal
        for (int r = 0, c = 7; c > 2; r++, c--) {
            if (matrix[r][c] == 3 && matrix[r + 1][c - 1] == 3 && matrix[r + 2][c - 2] == 3) {
                System.out.println("Got at least three consecutive 3's in subdiagonal ");
                break;
            }
        }//end of subdiagonal for loop for 3

    }//end of findSub method
}//end of main class

Edited 5 Years Ago by 24x24: n/a

This question has already been answered. Start a new discussion instead.