2D Arrays.

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Advertisements

1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Manipulating 2D arrays in Java
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Chapter VII: Arrays.
Arrays.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Two-Dimensional Arrays
Two-Dimensional Arrays
1.5 Matricies.
CS100J Lecture 19 Previous Lecture This Lecture
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Sit-In Lab 1 Ob-CHESS-ion
Java Software Structures: John Lewis & Joseph Chase
CS 200 Arrays, Loops and Methods
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSS161: Fundamentals of Computing
CS 200 Arrays, Loops and Methods
Nested Loop Review and Two-Dimensional Arrays
CS Week 9 Jim Williams, PhD.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
More 2 D Array.
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Multidimensional array
Building Java Programs
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Repetition Statements
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Outline Declaring and Using Arrays Arrays of Objects
Arrays and Matrices Prof. Abdul Hameed.
How do you do the following?
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Arrays.
Presentation transcript:

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 int twoDimArray = new int[3][2]; // 3-dimensional array with 3*2*4 = 24 elements String threeDimArray = new int[3][2][4];

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

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}; array[0][1] array[1][0] array[1][1] array[0][0] array[0][2] array[2][0] array[2][2] array[3][0] array[1][2] array[2][1] array[3][1] array[3][2] 63 28 13 21 29 91 38 41 10 70 52 39 1 2 41 38 91 63 10 21 29 28 13 3 70 52 39 Col Index Row Index

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 41 38 91 63 10 21 29 28 13 70 52 39 41 38 91 63 10 21 29 28 13 70 52 39

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

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 (int rowNum = 0; rowNum < rows; rowNum++) { for (int colNum = 0; colNum < columns; colNum++) System.out.print( array[row][column] + “\t“ ); System.out.println(); }

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

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

View – The Interface Views change Rules don’t 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

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

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

BINGO How would we represent BINGO as a 2D array? B: 1-15 I: 16-30 No number can repeat

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 boolean checkVLine(int[][] card, int col, int[] called) public static boolean checkHLine(int[][] card, int row, int[] called) public static boolean checkTLtoBR(int[][] card, int[] called)//Top Left to Bottom Right public static boolean checkTRtoBL(int[][] card, int[] called)//Top Right to Bottom Left public static boolean isWinner(int[][] card, int[] called) public static int[] subArray(int[] arr, int from, int to)

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

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(int i = 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;

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(); }

BINGO Lines public static boolean checkVLine(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

BINGO Lines public static boolean checkHLine(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

BINGO Lines public static boolean checkTLtoBR(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

BINGO Lines public static boolean checkTRtoBL(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

BINGO – Better Line public static boolean checkLine(int[][] card, int[] called, int r, int c, int dr, 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

BINGO public static boolean isWinner(int[][] card, int[] called) { boolean makesLine = 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; }

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

BINGO Main public static void main(String[] args) { int trials = 1000; int numbersCalled = 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”); }

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