Progress on diagonal searching

This commit is contained in:
2023-01-19 13:59:42 -06:00
parent 7fd9b8a489
commit 44950337e6
6 changed files with 520 additions and 12 deletions

View File

@@ -11,7 +11,8 @@ import java.util.Scanner;
*/
public class MP1_CalebFontenot {
public static int[][] inputArray() {
public static int[][] inputArray()
{
int m, n, i, j;
// Create scanner
Scanner input = new Scanner(System.in);
@@ -42,11 +43,22 @@ public class MP1_CalebFontenot {
return null;
}
public static boolean isConsecutiveFour(int[] values)
{
for (int i = 0; i < values.length - 4; ++i) {
if (values[i] == values[i + 1]
&& values[i + 1] == values[i + 2]
&& values[i + 2] == values[i + 3]) {
return true;
} else {
return false;
}
}
return false;
}
public static void main(String[] args) { //2965
public static void main(String[] args)
{ //2965
int[][] horizontalTestArray = { // Horizontal
{0, 1, 0, 3, 1, 6, 1},
{0, 1, 6, 8, 6, 0, 1},
@@ -131,7 +143,8 @@ public class MP1_CalebFontenot {
//System.out.println(isConsecutiveFour(intArray1));
}
public static void printArray(int[][] array) {
public static void printArray(int[][] array)
{
int rowCounter = 0;
for (int x = 0; x < array.length; x++) {
for (int y = 0; y < array[x].length; y++) {
@@ -144,7 +157,15 @@ public class MP1_CalebFontenot {
}
}
public static boolean isConsecutiveFour(int[][] values) {
public static void printArray(int[] array)
{
for (int i : array) {
System.out.print(i + " ");
}
}
public static boolean isConsecutiveFour(int[][] values)
{
int intCounter = 0, y = 0;
// Horizontal checking
// If the same value has been observed 4 times, return true
@@ -190,14 +211,24 @@ public class MP1_CalebFontenot {
System.out.println("Checking next line...");
}
System.out.println("Checking for Diagonal matches...");
for (int rowIterate = 0; rowIterate < (values.length - 4); ++rowIterate) {
for (int x = 0; x < values.length - 1; ++x) {
if (values[x][rowIterate] == values[x][rowIterate + 1]) {
}
int rows = 0;
for (int numColumns = 0; numColumns < values[0].length; numColumns++) {
int i = 0;
int j = numColumns;
int[] _1DArray = new int[numColumns + 1];
for (int numRows = 0; numRows <= rows; ++numRows) {
System.out.println("i: " + i + " j: " + j);
_1DArray[numRows] = values[i][j];
//printArray(_1DArray);
//System.out.println(values[rows][i] + " " + values[rows][j]);
++i;
--j;
}
System.out.println("Checking next line...");
rows++;
}
System.out.println("No match found.");
return false;
}

View File

@@ -0,0 +1,234 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp1_calebfontenot;
/**
*
* @author caleb
*/
public class MarkouCode {
/**
* Traverses the parm array diagonally from top left towards the top and
* prints the (i,j) indexes of the traversal.
*
* @param ar 2D array
*/
public static boolean topLeftTriangleDiagonalNothwest(int[][] ar)
{
System.out.println("diagonal indexes top-triangle of array ");
int rowCount = 0;
for (int columnCounter = 0; columnCounter < ar[0].length; ++columnCounter)
{
int i = 0;
int j = columnCounter;
int[] diagonalArray = new int[rowCount + 1];
for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter)
{
diagonalArray[diagonalCounter] = ar[i][j];
if (isConsecutiveFour(diagonalArray)) {
return true;
}
System.out.print(i + "," + j + " ");
++i;
j--;
if (i == ar.length | j == ar[0].length)
{
break;
}
}
rowCount++;
System.out.println("");
}
return false;
}
/*Traverses the parm array diagonally from bottom right towrads the to
//and prints the (i,j) indexes of the traversal.
*
* @param ar
*/
public static void bottomRightTriangleDiagonalNothwest(int[][] ar)
{
System.out.println("diagonal indexes bottom-triangle of array ");
int rowCount = 0;
int numColumns = 0;
if (ar[0].length == ar.length)//square table
{
numColumns = ar[0].length - 1;
}
else if (ar[0].length > ar.length)//wide table
{
numColumns = ar.length - 1;
}
else //narrow-width rectangle array
{
numColumns = ar[0].length;
}
for (int columnCounter = 0; columnCounter < numColumns; ++columnCounter)
{
int i = ar.length - 1;
int j = ar[0].length - 1 - columnCounter;
for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter)
{
System.out.print(i + "," + j + " ");
--i;
j++;
}
rowCount++;
System.out.println("");
}
//middle chunk of narrow array
System.out.println("-----------------------");
if (ar.length > ar[0].length)
{
System.out.println("diagonal indexes middle part of array when the array "
+ "is narrow ");
for (int i = 1; i < ar.length - ar[0].length; ++i)
{
int rowIndex = i;
int columnIndex = ar[0].length - 1;
for (int j = 0; j < ar[0].length; ++j)
{
System.out.print(rowIndex + "," + columnIndex + " ");
rowIndex++;
columnIndex--;
}
System.out.println("");
}
}
}
public static boolean isConsecutiveFour(int[][] values)
{
return true;
}
public static boolean isConsecutiveFour(int[] values)
{
for (int i = 0; i < values.length - 4; ++i) {
if ( values[i] == values[i+1] &&
values[i+1] == values[i+2] &&
values[i+2] == values[i+3]
) {
return true;
}
else {
return false;
}
}
return false;
}
private static void printIndexesDiagonally(int ar[][])
{
topLeftTriangleDiagonalNothwest(ar);
System.out.println("------------------------");
bottomRightTriangleDiagonalNothwest(ar);
}
public static void main(String[] args)
{
int[][] ar1 =
{
{
1, 1, 1, 1, 8
},
{
1, 1, 1, 8, 9
},
{
1, 1, 8, 1, 9
},
{
1, 8, 1, 3, 2
},
};
int[][] ar2 =
{
{
1, 1, 1, 1, 1, 1, 1, 1
},
{
1, 1, 1, 1, 1, 1, 1, 1
},
{
1, 1, 1, 1, 1, 1, 1, 1
},
{
1, 1, 1, 1, 1, 1, 1, 1
},
{
1, 1, 1, 1, 1, 1, 1, 1
},
};
int[][] ar3 =
{
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
{
1, 1, 1
},
};
System.out.println(topLeftTriangleDiagonalNothwest(ar1));
/*
System.out.println("SQUARE array of size 4x4");
printIndexesDiagonally(ar1);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("\nWIDE array of size 8x5");
printIndexesDiagonally(ar2);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("\nNARROW array of size 10x3");
printIndexesDiagonally(ar3);
*/
}
}