This java assignment is frying my brain and I was hoping someone could solve this for me and explain how they found the answer? I have been trying for hours to comprehend it :/ this stinks!
Assignment: Consecutive Equal Numbers
Problem Description:
Write the following function that tests whether a two-dimensional list has four consecutive numbers of the same value, either horizontally, vertically, or diagonally.
public static boolean isConsecutiveFour(int[][] values)
Write a test program that prompts the user to enter the number of rows and columns of a two-dimensional list and then the values in the list. The program will display Found four consecutive occurrences of n. if the list contains four consecutive numbers with the same value n. Otherwise, it will display Did not find four consecutive occurrences.
I pretty much tried to understand arrays but there is so much terminology and numbers and it destroyed my mind :/
This is tested on basic 5 x 5 inputs, and works.
It's just for-loops and conditional checks.
Let me know if you have any questions—I added comments above each test.
public class Example {
public static void main(String[] args) {
ui();
}
static int rows, columns;
static void ui() {
Scanner scanner = new Scanner(System.in);
System.out.print("enter rows and columns: ");
rows = scanner.nextInt();
columns = scanner.nextInt();
int[][] values = new int[rows][columns];
for (int row = 0; row < rows; row++) {
System.out.printf("enter row %d: ", row + 1);
for (int column = 0; column < columns; column++)
values[row][column] = scanner.nextInt();
}
System.out.println("isConsecutiveFour(values) = " + isConsecutiveFour(values));
}
public static boolean isConsecutiveFour(int[][] values) {
/* check horizontal */
int previous, count;
for (int[] row : values) {
previous = row[0];
count = 0;
for (int index = 1; index < row.length; index++) {
if (previous == row[index]) count++;
if (count == 3) return true;
previous = row[index];
}
}
/* check vertical */
for (int column = 0; column < columns; column++) {
previous = values[0][column];
count = 0;
for (int row = 1; row < rows; row++) {
if (previous == values[row][column]) count++;
if (count == 3) return true;
previous = values[row][column];
}
}
/* check horizontal, left to right */
for (int column = 0; column < columns; column++) {
previous = values[0][column];
count = 0;
for (int row = 1; row < rows; row++) {
if (column + 1 >= columns) break;
if (previous == values[row][++column]) count++;
if (count == 3) return true;
previous = values[row][column];
}
}
/* check horizontal, right to left */
for (int column = 0; column < columns; column++) {
previous = values[rows - 1][column];
count = 0;
if (rows - 2 < 0) return false;
for (int row = rows - 2; row >= 0; row--) {
if (column + 1 >= columns) break;
if (previous == values[row][++column]) count++;
if (count == 3) return true;
previous = values[row][column];
}
}
return false;
}
}
Example input and outputs.
These should pass.
enter rows and columns: 5 5
enter row 1: 0 1 2 3 4
enter row 2: 4 0 1 2 3
enter row 3: 3 4 0 1 2
enter row 4: 2 3 4 0 1
enter row 5: 1 2 3 4 0
isConsecutiveFour(values) = true
enter rows and columns: 5 5
enter row 1: 1 2 3 4 5
enter row 2: 1 2 3 4 5
enter row 3: 0 0 0 0 0
enter row 4: 1 2 3 4 5
enter row 5: 1 2 3 4 5
isConsecutiveFour(values) = true
enter rows and columns: 5 5
enter row 1: 1 2 0 3 4
enter row 2: 1 2 0 3 4
enter row 3: 2 1 0 4 3
enter row 4: 1 2 0 3 4
enter row 5: 1 2 0 3 4
isConsecutiveFour(values) = true
And, these should fail.
enter rows and columns: 5 5
enter row 1: 1 2 3 4 5
enter row 2: 5 4 3 2 1
enter row 3: 0 1 0 1 0
enter row 4: 5 4 3 2 1
enter row 5: 1 2 3 4 5
isConsecutiveFour(values) = false
enter rows and columns: 3 3
enter row 1: 1 1 1
enter row 2: 1 1 1
enter row 3: 1 1 1
isConsecutiveFour(values) = false
Test case
enter rows and columns: 6 7
enter row 1: 0 1 0 3 1 6 1
enter row 2: 0 1 6 8 6 0 1
enter row 3: 5 6 2 1 8 2 9
enter row 4: 6 5 6 1 1 9 1
enter row 5: 1 3 6 1 4 0 7
enter row 6: 3 3 3 3 4 0 7
isConsecutiveFour(values) = true