1 / 26

2D Arrays

2D Arrays. Multiple Indices in Arrays. Arrays may have any number of indices The number of indices is the array dimension Example of declarations: // 1-dimensional array with 10 elements double oneDimArray = new double[10]; // 2-dimensional array with 3*2 = 6 elements

Download Presentation

2D Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2D Arrays

  2. Multiple Indices in Arrays • Arrays may have any number of indices • The number of indices is the array dimension • Example of declarations: // 1-dimensional array with 10 elements double oneDimArray = new double[10]; // 2-dimensional array with 3*2 = 6 elements int twoDimArray = new int[3][2]; // 3-dimensional array with 3*2*4 = 24 elements String threeDimArray = new int[3][2][4];

  3. 2-Dimensional Arrays • Two-dimensional arrays are usually visualized as tables • Really arrays of arrays • Elements are organized in rows and columns • Initialized and stored in row-major order, i.e., the first index represents the rows and the second index represents the columns • The element [i][j] is the element at row index i and column index j

  4. array[0][0] 41 array[0][1] 38 array[0][2] 91 array[1][0] 63 array[1][1] 10 array[1][2] 21 array[2][0] 29 array[2][1] 28 array[2][2] 13 array[3][0] 70 array[3][1] 52 Row Index array[3][2] 39 Col Index Initializing 2D Arrays // A 2D array with 4 rows, // 3 columns, 12 elements int[] row0 = {41,38,91}, row1 = {63,10,021}, row2 = {29,28,13}, row3 = {70,52,39}; int array[][] = {row0, row1, row2, row3};

  5. Row Major Representation • So far, all 2D arrays shown have been in row major order. • All of the data for the first row is stored before the next row • There is also column major order • You should assume all 2D arrays are row-major for our course • Inner arrays represent columns

  6. Row vs. Column Major int[][] colM = { {1,4}, {2,5}, {3,6} }; int[][] rowM = {{1,2,3}, {4,5,6} };

  7. Accessing 2-D array elements int[] row0 = {41,38,91}, row1 = {63,10,021}, row2 = {29,28,13}, row3 = {70,52,39}; int array[][] = {row0, row1, row2, row3}; System.out.println( array[0][2] );//91 System.out.println( array[3][1] );// array[0][1] = array[1][1] + array[2][1]; System.out.println( array[0][1] );//10 + 28 = 38 int rows = array.length;//number of arrays = rows int columns = array[0].length;//number of elements in an array = columns //print all indicies in the “table” (2D array) for (introwNum = 0; rowNum < rows; rowNum++) { for (intcolNum = 0; colNum < columns; colNum++) System.out.print( array[row][column] + “\t“ ); System.out.println(); }

  8. Board Games • One of the concepts in CS12 is to understand how we organize memory to aid in developing our solutions • A natural organization for a board game is a 2D array

  9. Logic – The rules • Whose Turn? • Is a move legal? • Check before it is allowed • Is the game over? • Board full • No more legal moves • Who is the winner? • What is the score? • What piece is located at a given position? • Drawing/updating the board • Note that all of these questions are UI independent

  10. View – The Interface • Views change • New OS • New version of software • Updated graphics • … • Rules don’t • Your View of the game will be the only class that can reference any of the UI • Greenfoot or Swing is fine

  11. Good Design • Good design requires that you keep the logic (sometimes called the document) separate from the view • Your game can then be implemented on an interface relatively quickly when the underlying logic is available as a separate class • When the two are tangled, making changes can be very difficult/inefficient

  12. BINGO • Not a two player game but let’s try to write a couple of methods to help you get the hang of it • Make a new BINGO card • determine if a player has a valid winning card (1 line anywhere) • ? How many numbers should we expect to be called before we get a BINGO • Lucky vs. Unlucky

  13. BINGO • How would we represent BINGO as a 2D array? • B: 1-15 • I: 16-30 • N: 31-45 • G: 46-60 • O: 61-75 • No number can repeat

  14. BINGO public static int[] fill(int from, int to) public static void shuffle(int[] arr) public static int[][] makeCard() public static void print(int[][] card) public static booleancheckVLine(int[][] card, int col, int[] called) public static booleancheckHLine(int[][] card, int row, int[] called) public static booleancheckTLtoBR(int[][] card, int[] called)//Top Left to Bottom Right public static booleancheckTRtoBL(int[][] card, int[] called)//Top Right to Bottom Left public static booleanisWinner(int[][] card, int[] called) public static int[] subArray(int[] arr, int from, int to)

  15. Bingo Helpers public static int[] fill(int from, int to) { int[] arr = new int[to – from + 1]; for(inti = from; i <= to; i++) arr[i - from] = i; return arr; } public static void shuffle(int[] arr) { for(inti = arr.length-1; i > 0; i--) { int target = (int)(Math.random()*(i+1)); int temp = arr[i]; arr[i] = arr[target]; arr[target] = temp; } }

  16. Bingo public static int[][] makeCard() { int rows = 5; int cols = 5; int[][] bingo = new int[rows][cols]; int start = 1; int end = 15; int column = 0; while(end <= 75) { int[] temp = fill(start, end); shuffle(temp); for(inti = 0; i < rows; i++) bingo[i][column] = temp[i]; column++; start = end + 1; end += 15; } bingo[rows/2][cols/2] = 0;//free space, sentinel value return bingo; }

  17. BINGO public static void print(int[][] card) { int rows = card.length; int cols = card[0].length; for(int r = 0; r < rows; r++) { for(int c = 0; c < cols; c++) System.out.print(card[r][c] + “\t”); System.out.println(); } }

  18. BINGO Lines public static booleancheckVLine(int[][] card, int col, int[] called) { int rows = card.length; int r = 0; while(r < rows) { if(!(r == rows/2 && col == cols/2))//not free square { boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][col] == called[n]; if(!found)//card[r][col] does not contain a called number return false; } r++; } return true;//all numbers were called or false would be returned }

  19. BINGO Lines public static booleancheckHLine(int[][] card, int row, int[] called) { int cols = card.length; int c = 0; while(c < cols) { if(!(row == rows/2 && c == cols/2))//not free square { boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[row][c] == called[n]; if(!found)//card[row][c] does not contain a called number return false; } c++; } return true;//all numbers were called or false would be returned }

  20. BINGO Lines public static booleancheckTLtoBR(int[][] card, int[] called)//Top Left to Bottom Right { int rows = card[0].length; int cols = card.length; int r = 0; int c = 0; while(r < rows && c < cols) { if(!(r == rows/2 && c == cols/2))//not free square { boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][c] == called[n]; if(!found)//card[r][c] does not contain a called number return false; } r++; c++; } return true;//all numbers called }

  21. BINGO Lines public static booleancheckTRtoBL(int[][] card, int[] called)//Top Right to Bottom Left { int rows = card[0].length; int cols = card.length; int r = 0; int c = cols - 1; while(r < rows && c >= 0) { if(!(r == rows/2 && c == cols/2))//not free square { boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][c] == called[n]; if(!found)//card[r][c] does not contain a called number return false; } r++; c--; } return true;//all numbers called }

  22. BINGO – Better Line public static booleancheckLine(int[][] card, int[] called, int r, int c, intdr, int dc) { int rows = card[0].length; int cols = card.length; while(r < rows && r >= 0 && c < cols && c >= 0)//in bounds { if(!(r == rows/2 && c == cols/2))//not free square { boolean found = false; for(int n = 0; n < called.length && !found; n++) found = card[r][c] == called[n]; if(!found)//card[r][c] does not contain a called number return false; } r += dr; c += dc; } return true;//all numbers called }

  23. BINGO public static booleanisWinner(int[][] card, int[] called) { booleanmakesLine = false; for(int r = 0; r < card.length && !makesLine; r++)//horizontal lines makesLine = checkLine(card, called, r, 0, 0, 1); for(int c = 0; c < card[0].length && !makesLine; c++)//vertical lines makesLine = checkLine(card, called, 0, c, 1, 0); if(!makesLine) makesLine = checkLine(card, called, 0,0,1,1) || checkLine(card, called, 0, card[0].length, 1,-1); return makesLine; }

  24. BINGO - Main public static int[] subArray(int[] arr, int from, int to) { int[] sub = new int[to-from+1]; for(inti = from; i <= to; i++) sub[i-from] = arr[i]; return sub; }

  25. BINGO Main public static void main(String[] args) { int trials = 1000; intnumbersCalled = 0; int counter = 0; while(counter < trials) { int[][] card = makeCard(); int[] called = fill(1,75); shuffle(called); int game = 0; while(!isWinner(card, subArray(called, 0, game) )) game++; numbersCalled += game + 1;//game is 0-based counter++; } double ave = (double)numbersCalled/trials; System.out.println(ave+ “ = average number of call outs to win”);}

  26. Expectations • Fluency in manipulating a 2D array • Looping over/through values in the array • In distinctly different ways (scrolling vertically vs horizontally is not a different way) • Scanning board for score • Scanning board to enforce rules • Scanning board to make changes • Accessing elements of the array • You will be expected to transfer your skills to a similar situation, that uses a 2D array for your unit test

More Related