/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package test3array2dcheck; import java.util.ArrayList; /** * * @author jcmtiernan */ public class Test3Array2DCheck { /** * @param args the command line arguments */ public static void main(String[] args) { final int MAXCOL = 4; int[][] matrix = new int[MAXCOL][MAXCOL]; int firstVal = 3; int val = firstVal; for (int r = 0; r < MAXCOL; r++) for (int c = 0; c < MAXCOL; c++) { matrix[r][c] = val; val = val + firstVal; } printSqMatrix(matrix); int[][] myTable = matrixmod(matrix); printSqMatrix(myTable); int[] vals = new int[MAXCOL*MAXCOL]; int index = 0; for (int r = 0; r < MAXCOL; r++) for (int c = (MAXCOL-1); c >= 0; c--) { vals[index++] = matrix[c][r]; } System.out.println("\nArray to check"); for (int i=0; i < vals.length; i++) { System.out.print(vals[i]+ (( i % MAXCOL == (MAXCOL-1) ) ? "\n" : "\t" )); } System.out.println("\nFind min recursively"); System.out.println("Min is "+gMan(vals, vals.length)); /* System.out.println("\nAnother recursive example - mystery(n,m,p)"); System.out.println("Mystery(2, 4, 0) = "+mystery(2,4,0)); System.out.println("Mystery(2, 4, 1) = "+mystery(2,4,1)); System.out.println("Mystery(2, 4, 2) = "+mystery(2,4,2)); */ System.out.println("\nAnother recursive example - palindrome(string)"); System.out.println("whatIs(receiver) = "+whatIs("receiver")); System.out.println("whatIs(racecar) = "+whatIs("racecar")); System.out.println("whatIs(repair) = "+whatIs("repair")); System.out.println("whatIs(radar) = "+whatIs("radar")); int[][] shNumsOnly = {{1962, 1, 2003, 244, 630}, {1941, 8, 2017, 175, 57}, {1939, 27, 1989, 178, 77}, {1942, 5, 0, 185, 81}, {2000, 1, 1967, 170, 57}}; int[][] shNums7 = {{1962, 1, 2003, 244, 630}, {1941, 8, 2017, 175, 57}, {1939, 27, 1989, 178, 77}, {1942, 5, 0, 185, 81}, {2000, 1, 1967, 170, 57}, {1975, 5, 1996, 178, 81}, {2012, 17, 0, 158, 69}}; int[][] seats = shNumsOnly.clone(); System.out.println("Matrix for sort test"); print2DTable(shNumsOnly); someSort(shNumsOnly); System.out.println("\nMatrix after someSort "); print2DTable(shNumsOnly); /* for (int r = 0; r < shNumsOnly.length; r++) { for (int c = 0; c < shNumsOnly[0].length; c++) { System.out.print(shNumsOnly[r][c]+"\t"); } System.out.println(""); } */ // testing airlineSeatRow(int[][] seats) System.out.println("\nMatrix for seats test. Non-zero means empty seat"); for (int r= 0; r < seats.length; r++) for (int c=0; c < seats[0].length; c++) { while ((seats[r][c] != 0) && (seats[r][c] < 200)) { seats[r][c] = seats[r][c] * 3; } while ((seats[r][c] != 0) && (seats[r][c] > 1200)) { seats[r][c] = seats[r][c] / 2; } } print2DTable(seats); /* for (int r = 0; r < shNumsOnly.length; r++) { for (int c = 0; c < shNumsOnly[0].length; c++) { System.out.print(seats[r][c]+"\t"); } System.out.println(""); } */ int row = airlineSeatRow(seats); System.out.println(""); System.out.println(((row != -1)?("First row with available seat is "+row):"No seats available") + "\n"); someSort(seats, 1); System.out.println("\nResorted and full rows added matrix for seats test. Non-zero means empty seat"); for (int r = 0; r < 2; r++) for (int c = 0; c < seats[0].length; c++) { seats[r][c] = 0; } print2DTable(seats); row = airlineSeatRow(seats); System.out.println(""); System.out.println(((row != -1)?("First row with available seat is "+row):"No seats available") + "\n"); String[] name1 = { "Batman", "Bruce", "Wayne"}; String[] name2 = {"Spiderman", "Peter", "Parker"}; String[] name3 = {"Wonder Woman", "Diana","Prince"}; String[][] rollArray = {name3, name2, name1}; ArrayList roll = new ArrayList<>(); roll.add(name1); roll.add(name3); roll.add(name2); System.out.println("ArrayList roll: "); for (String[] nm: roll) for (int n=0; n< nm.length; n++) System.out.print(nm[n]+ (( n % nm.length == (nm.length-1) ) ? "\n" : "\t" )); heroOut(roll, "Batman"); System.out.println("\nArrayList roll minus Batman: "); for (String[] nm: roll) for (int n=0; n< nm.length; n++) System.out.print(nm[n]+ (( n % nm.length == (nm.length-1) ) ? "\n" : "\t" )); heroOut(roll, "Batgirl"); System.out.println("\nArrayList roll minus Batgirl: "); for (String[] nm: roll) for (int n=0; n< nm.length; n++) System.out.print(nm[n]+ (( n % nm.length == (nm.length-1) ) ? "\n" : "\t" )); System.out.println("\nArray[] roll: "); for (String[] nm: rollArray) for (int n=0; n< nm.length; n++) System.out.print(nm[n]+ (( n % nm.length == (nm.length-1) ) ? "\n" : "\t" )); heroOut(rollArray, "Batgirl"); System.out.println("\nArray[][] roll minus Batgirl: "); for (String[] nm: rollArray) for (int n=0; n< nm.length; n++) System.out.print(nm[n]+ (( n % nm.length == (nm.length-1) ) ? "\n" : "\t" )); heroOut(rollArray, "Spiderman"); System.out.println("\nArray[][] roll minus Spiderman: "); for (String[] nm: rollArray) for (int n=0; n< nm.length; n++) System.out.print(nm[n]+ (( n % nm.length == (nm.length-1) ) ? "\n" : "\t" )); /* System.out.println("\nTable testing transpose again non square matrix"); print2DTable(shNums7); int[][] shNmTranspose = matrixmod(shNums7); System.out.println("\nTransposed matrix"); print2DTable(shNmTranspose); int[][] matrix2 = {{4, 8, 12}, {5, 10, 15}, {6, 12, 18}}; System.out.println("\nXC matrix testing transpose again non square matrix"); print2DTable(matrix2); int[][] matrix2t = matrixmod(matrix2); System.out.println("\nXC matrix testing transpose result"); print2DTable(matrix2t); */ } public static void heroOut(ArrayList roll, String shName) { int i = -1; String[] names; if (roll.size() == 0) System.out.println("\nNo heroes"); for (i=0; i < roll.size(); i++) { names = roll.get(i); if (names[0].equals(shName)) { roll.remove(i); System.out.println("\nEliminated a hero "+shName); i = roll.size() + 1; } } if (i == roll.size()) System.out.println("\nNot a hero "+shName); } public static void heroOut(String [][] roll, String shName) { int loc = roll.length; // location to remove element String[] blankNames = {"","",""}; if (loc == 0) // array has no elements System.out.println("\nNo heroes"); else { for (int i= 0; ((i < roll.length) && (loc == roll.length)); i++) { if (roll[i][0].equals(shName)) loc = i; // found element at index i, saving as loc } if (loc < roll.length) // if element was found, i.e. an index was found in the array { for(int r = loc; r < (roll.length-1); r++ ) // for all the elements from loc to the end roll[r] = roll[r+1]; // replace element at r with element at r+1 roll[roll.length-1] = blankNames; // blanks in last element System.out.println("\nEliminated a hero "+shName); } else System.out.println("\nNot a hero "+shName); } } public static int deleteElement( String[][] list,int sz, String element) { int loc = sz; for (int i= 0; ((i < sz) && (loc == sz)); i++) { if (list[i][0].equals(element)) loc = i; } if (loc < sz) { for(int r = loc; r < sz; r++ ) list[r] = list[r+1]; } if (loc != sz) sz--; // element was in the array return sz; // return new size of array } public static void print2DTable(int[][] seats) { for (int r = 0; r < seats.length; r++) { for (int c = 0; c < seats[0].length; c++) { System.out.print(seats[r][c]+"\t"); } System.out.println(""); } } public static int airlineSeatRow(int[][] seats) { int row = -1; int countEmpty = 0; for (int r= 0; r < seats.length; r++) for (int c=0; c < seats[0].length; c++) if (seats[r][c] != 0) return r; return row; } public static void someSort(int[][] array2D) { int colNum = array2D[0].length - 3; for (int pass = 0; pass < array2D.length; pass++) for (int comp = 0; comp < array2D.length -1 ; comp++) if (array2D[comp][colNum] < array2D[comp+1][colNum]) swap(array2D,comp); } public static void someSort(int[][] array2D, int colNum) { for (int pass = 0; pass < array2D.length; pass++) for (int comp = 0; comp < array2D.length -1 ; comp++) if (array2D[comp][colNum] < array2D[comp+1][colNum]) swap(array2D,comp); } private static void swap(int[][] list, int comp) { int[] temp; temp = list[comp]; list[comp] = list[comp+1]; list[comp+1] = temp; } public static int[][] matrixmod(int[][] matrix) { final int tr = matrix.length; final int tc = matrix[0].length; int[][] matrix2 = new int[tc][tr]; for (int r = 0; r < matrix.length; r++) for (int c = 0; c < matrix[0].length; c++) matrix2[c][r] = matrix[r][c]; return matrix2; } /* int getMin(int arr[], int n) { // If there is single element, return it. // Else return minimum of first element and // minimum of remaining array. return (n == 1) ? arr[0] : min(arr[0], getMin(arr + 1, n - 1)); } */ public static int gMan(int arr[], int n) { if (n == 1) return arr[arr.length - n]; else { return Math.min(arr[arr.length - n], gMan(arr, n - 1)); } } /* int mystery(n, m, p) { int i, result = 0; if (p==0) return n+m; for (i=0; i< m; i++) result += mystery(result,n,p-1); return result; } n = 2, m = 3, p = 0; mys = 2 + 3 = 5 n = 2, m = 3, p = 1; i = 0 res = 0; res = 0 + mys(0, 2, 0) = 2 i = 1 res = 2; res = 2 + mys(2, 2, 0) = 6 i = 2 res = 4; res = 6 + mys(6, 2, 0) = 14 When p==0, this is an addition function. When p==1, this is a multiplication function. When p==2, this is a power function. https://www.sparknotes.com/cs/recursion/examples/problems_3/ {Nope, not the way it is written} */ /* public static int mystery(int n, int m, int p) { int i = 0; int result = 0; if (p==0) return n + m; for (i=0; i < m; i++) result = mystery(result, n, p-1); return result; } */ /* function isPalindrome(str){ if(str.length === 0) { return true } if(str[0] !== str[str.length-1]){ return false } return isPalindrome(str.slice(1, str.length-1)) } */ public static boolean whatIs2(String str) { if((str.length() == 0) || (str.length() == 1)) { return true; } if(str.charAt(0) != str.charAt(str.length()-1)) { return false; } return whatIs(str.substring(1, str.length()-1)); } public static boolean whatIs(String str) { if((str.length() == 0) || (str.length() == 1)) return true; if(str.charAt(0) == str.charAt(str.length()-1)) { return whatIs(str.substring(1, str.length()-1)); } else return false; } public static void printSqMatrix(int[][] matrix) { System.out.println("MAXCOL="+matrix.length+", firstVal = "+matrix[0][0]); for (int t = 0; t < matrix.length; t++) System.out.print("\t["+t+"]"); for (int r = 0; r < matrix.length; r++) { System.out.print("\n["+r+"]"); for (int c = 0; c < matrix.length; c++) System.out.print("\t"+matrix[r][c]); } System.out.println(""); } }