Topic 26 Two Dimensional Arrays

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
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;
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Arrays.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
ARRAYS Multidimensional realities Image courtesy of
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 1 2 What is a Cellular Automaton? A one-dimensional cellular automaton (CA) consists of two things: a row of "cells" and a set of "rules". Each of.
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.
Department of Computer Science Western Michigan University
Linear Containers Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?
Lecture 18: Nested Loops and Two-Dimensional Arrays
Array in C# Array in C# RIHS Arshad Khan
Objects as a programming concept
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Two-Dimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Topic Dimensional Arrays
Computer Programming BCT 1113
Two Dimensional Arrays
Two-dimensional arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Computer Science 3 Hobart College
A simple way to organize data
Week 9 - Monday CS 121.
Two-Dimensional Arrays
Two Dimensional Arrays
7 Arrays.
Two-Dimensional Arrays
Ionut Trestian Northwestern University
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
More 2 D Array.
Programming Assignment #6
2D Array and Matrix.
Arrays ICS2O.
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Multidimensional Arrays
Multidimensional array
Computer Architecture and Assembly Language
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
Pointers Pointers point to memory locations
Northwestern University
Modeling Pattern Formation in Skin Diseases by a Cellular Automaton
Continue with Life, Magic and Catch -up
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Computer Architecture and Assembly Language
Multidimensional Arrays Section 6.4
AP Java Learning Objectives
C++ Array 1.
Topic 25 - more array algorithms
2D Array and Matrix Application: Game of Life
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it." -Alfred Aho and Jeffery Ullman Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/

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. by convention, in a 2D array the first number indicates the row and the second the column

Two Dimensional Arrays 0 1 2 3 column 1 2 row 0 0 0 0 0 0 0 0 This is our abstract picture of the 2D array and treating it this way is acceptable. (actual implementation is different) mat[2][1] = 12;

What is What? // single elements // no way to refer to a single column int[][] mat = new int[10][12]; // mat is a reference to the whole 2d array // mat[0] or mat[r] are references to a single row // mat[0][1] or mat[r][c] are references to // single elements // no way to refer to a single column

2D Array Problems Write a method to find the max value in a 2d array of ints Write a method that finds the sum of values in each column of a 2d array of doubles Write a method to print out the elements of a 2d array of ints in row order. row 0, then row 1, then row 2 ... Write a method to print out the elements of a 2d array of ints in column order column 0, then column 1, then column 2 ...

clicker question 40 0 0 8 5 0 5 8 0 5 8 then a runtime error occurs What is output by the following code? String[][] strTable = new String[5][8]; System.out.print(strTable.length + " "); System.out.print(strTable[0].length + " "); System.out.print(strTable[2][3].length()); 40 0 0 8 5 0 5 8 0 5 8 then a runtime error occurs No output due to a syntax error.

Use of Two Dimensional Arrays 2D arrays are often used when I need a table of data or want to represent things that have 2 dimensions. For instance an area of a simulation

Example of using a 2D array Conway's Game of Life a cellular automaton designed by John Conway, a mathematician not really a game a simulation takes place on a 2d grid each element of the grid is occupied or empty

Simulation http://www.cuug.ab.ca/dewara/life/life.html www.ibiblio.org/lifepatterns/

Generation 0 0 1 2 3 4 5 . * . * . * 1 * . * * * * 2 3 . . * * . * 0 1 2 3 4 5 1 2 3 . * . * . * * . * * * * . . * * . * . * * * . * * indicates occupied, . indicates empty

Or 0 1 2 3 4 5 1 2 3

Generation 1 0 1 2 3 4 5 1 2 3 . * . * . * . . . . . * . . . . . * . * . * . . * indicates occupied, . indicates empty

Or , Generation 1 0 1 2 3 4 5 1 2 3

Rules of the Game If a cell is occupied in this generation. it survives if it has 2 or 3 neighbors in this generation it dies if it has 0 or 1 neighbors in this generation it dies if it has 4 or more neighbors in this generation If a cell is unoccupied in this generation. there is a birth if it has exactly 3 neighboring cells that are occupied in this generation Neighboring cells are up, down, left, right, and diagonal. In general a cell has 8 neighboring cells

Clicker Question Implement a program to run the simulation What data type do you want to use for the elements of the 2d array? String char int boolean double

Do you want to use a buffer zone on the edges? Clicker Question Do you want to use a buffer zone on the edges? No Yes