Topic Dimensional Arrays

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
CS305j Introduction to Computing Two Dimensional Arrays 1 Topic 22 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right.
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
CSC – Java Programming II Lecture 9 January 30, 2002.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
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!
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays Chapter 7.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Chapter 6: Using Arrays.
Java for Beginners Level 6 University Greenwich Computing At School
Two-Dimensional Arrays
Two Dimensional Arrays
Two-dimensional arrays
Two Dimensional Array Mr. Jacobs.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
JavaScript: Functions.
Week 9 - Monday CS 121.
Two-Dimensional Arrays
A+ Computer Science AP Review 2018 AP CS A EXAM
7.3 Matrices.
Nested Loop Review and Two-Dimensional Arrays
Topic 26 Two Dimensional Arrays
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.
EKT150 : Computer Programming
Topics Covered: Arrays, 1-D & 2-D Passing & Returning Arrays
Multidimensional Arrays
The Matrix A b a d e a a a a a a a A b a d e a a a a a a a
Starting Out with Programming Logic & Design
2D Array and Matrix.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional array
Lecture 12: 2D Arrays AP Computer Science Principles
Multi-Dimensional Arrays
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Multidimensional Arrays Section 6.4
C++ Array 1.
A+ Computer Science AP Review 2019 AP CS A EXAM
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Lec 21 More Fun with Arrays: For Loops
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Arrays.
Announcements.
Announcements Exam 2 Next Week!! Final Exam :
Presentation transcript:

Topic 2 - 2 Dimensional Arrays

2D Arrays in Java Arrays with multiple dimensions may be declared and used int[][] mat = new int[3][4]; the number of pairs of square brackets indicates the dimension of the array. could have an array of 3 dimensions, or 4 or more, but AP only covers arrays of 1 or 2 dimensions Topic 2 - 2D Arrays

Number of Rows and Columns Once 2D array created access elements with 2 subscripts int[][] mat = new int[3][4]; by convention, in a 2D array the first subscript indicates the row number and the second the column number mat has 3 rows and 4 columns per row Topic 2 - 2D Arrays

Coordinates of a Cell When accessing elements 2 subscripts are needed the first subscript indicates the row the second subscript indicates the column int[][] mat = new int[3][4]; mat[2][3] = 12; column row Topic 2 - 2D Arrays

2 Dimensional Arrays row 0 1 2 3 column 0 0 0 0 1 0 0 0 0 2 1 2 0 0 0 0 0 0 0 0 This is our abstract picture of the 2D array mat[2][1] = 12; Topic 2 - 2D Arrays

The Real Picture 0 1 2 3 0 0 0 0 1 2 0 1 2 3 0 0 0 0 mat 0 1 2 3 0 0 0 0 mat holds the memory address of an array with 3 elements. Each element holds the memory address of an array of 4 ints Topic 2 - 2D Arrays

Arrays of Multiple Dimension because multiple dimensional arrays are treated as arrays of arrays of arrays……multiple dimensional arrays can be ragged each row does not have to have the same number of columns each row array has its own length variable this is rarely used and is not tested on APCS int[][] raggedMat = new int[5][]; for(int i = 0; i < raggedMat.length; i++) raggedMat[i] = new int[i + 1]; Topic 2 - 2D Arrays

Rectangular 2D Arrays Ragged arrays are sometime useful, but normally we deal with rectangular 2D arrays each row has the same number of columns as every other row use this a lot as precondition to methods that work on matrices given int[][] mat mat.length refers to the number of rows in the 2D array mat[0].length refers to the number of columns per row Topic 2 - 2D Arrays

Working with 2D Arrays It is very common to have to access all elements of a 2D array Simplest way to do this is with a nested for loop. Topic 2 - 2D Arrays

Example of nested for Loops String temp; for(int i = 0; i < 4; i++) { for(int j = 0; j < 3; j++) { temp = "[" + i + "," + j + "] "; System.out.print( temp ); } System.out.println(); What is the output? outer loop inner loop Topic 2 - 2D Arrays

Output of Example [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2] [3,0] [3,1] [3,2] What would out be if the println statement were removed? Topic 2 - 2D Arrays

2D Array Example Find the maximum value in a 2D array of ints must search every element public int findMax(int[][] mat) { /* pre: mat != null, mat.length > 0, mat[0].length > 0, mat is rectangular post: return maximum element in mat */ Topic 2 - 2D Arrays

findMax Implemented public int findMax(int[][] mat) { int max = mat[0][0]; for(int r = 0; r < mat.length; r++) { for(int c = 0; c < mat[0].length; c++) { if( mat[r][c] > max ) { max = mat[r][c]; } return max; Topic 2 - 2D Arrays

Conway' Game of Life A more complicated 2D Array problem Conway's Game of Life www.math.com/students/wonders/life/life.html cells are either occupied by an organism or empty next generation depends on the current status (occupied or empty) and your 8 neighboring cells occupied cell 0 - 1 neighbors, organism dies (loneliness) 2 - 3 neighbors, organism lives >= 4 neighbors, organism dies (over crowding) empty cell 3 neighbors birth, organism born Topic 2 - 2D Arrays

Game of Life Example 0 1 2 3 4 5 1 2 3 4 5 . . . . . . . * * . . . . . * . * . . * . * . . . . . * . . "*" -> occupied, "." -> empty, generation 0 Topic 2 - 2D Arrays

Game of Life 0 1 2 3 4 5 1 2 3 4 5 . 1 . 2 . 2 . 1 . 0 . 0 . 1 * 2 * 2 . 3 . 1 . 1 . 1 . 4 * 4 . 4 * 1 . 1 . 1 * 2 . 4 * 4 . 3 . 2 . 1 . 2 * 2 . 4 * 2 . 1 . 0 . 1 . 2 * 2 . 2 . 1 added number of neighboring cells that are occupied Topic 2 - 2D Arrays

Game of Life 0 1 2 3 4 5 1 2 3 4 5 . . . . . . . * * * . . . * . . * . . . * . * . . . . * . . apply all changes simultaneously, generation 1 Topic 2 - 2D Arrays

Game of Life 0 1 2 3 4 5 1 2 3 4 5 . . * . . . . * . * . . . . . * . . . . * . * . apply all changes simultaneously, generation 2 Topic 2 - 2D Arrays

Game of Life 0 1 2 3 4 5 1 2 3 4 5 . . . . . . . * * * . . . . . * . . . . . * * . . . * . * . apply all changes simultaneously, generation 3 Topic 2 - 2D Arrays

Game of Life 0 1 2 3 4 5 1 2 3 4 5 . . * . . . . . * * . . . . . . . . . . * . * . . . . * . . apply all changes simultaneously, generation 4 Topic 2 - 2D Arrays

Game of Life 0 1 2 3 4 5 1 2 3 4 5 . . * * . . . . * . . . . . . . . . . . * . * . . . . * . . apply all changes simultaneously, generation 5 (and so forth…) Topic 2 - 2D Arrays

Game of Life nextGen method Write a method that returns a 2D array of booleans that represents the next generation based on the current generation public boolean[][] nextGen(boolean[][] world) { /* pre: world != null, mat.length > 0, mat[0].length > 0, world is rectangular post: return next generation of world based on rules of Game of Life */ Topic 2 - 2D Arrays

nextGen source code public boolean[][] nextGen(int[][] world) { final int ROWS = world.length; final int COLS = world[0].length; boolean[][] result = new boolean[ROWS][COLS]; int num; for(int r = 0; r < ROWS; r++) { for(int c = 0; c < COLS; c++) { num = getNumNeighbors(world, r, c); //check for alive and survives if( world[row][col] && ( num == 2 || num == 3) result[row][col] = true; // check for birth else if( !world[row][col] && num == 3) } Topic 2 - 2D Arrays

2D Array in the MBCS The AP Marine Biology Case Study uses a 2D array in its BoundedEnv class the BoundedEnv class represents an area of water in which the fish in the MBCS are located instance variables for BoundedEnv private Locatable[][] theGrid; private int objectCount; Topic 2 - 2D Arrays

the allObjects method a method in the BoundedEnv class returns an array with a reference to everything in the BoundedEnv typical 2D array processing (nested loop) makes use of helper methods instead of accessing length variables directly (numRows, numCols) Topic 2 - 2D Arrays

the allObjects source code /** pre: none * Returns all the objects in this environment. * @return an array of all the environment objects **/ public Locatable[] allObjects() { Locatable[] theObjects = new Locatable[numObjects()]; int tempObjectCount = 0;   // Look at all grid locations. for ( int r = 0; r < numRows(); r++ ) { for ( int c = 0; c < numCols(); c++ ) { // If there's an object at this location, // put it in the array. Locatable obj = theGrid[r][c]; if ( obj != null ) { theObjects[tempObjectCount] = obj; tempObjectCount++; } return theObjects; Topic 2 - 2D Arrays