Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
Advertisements

Arrays.
Programming project #2 1 CS502 Spring 2006 Programming Project #2 CS-502 Operating Systems Spring 2006.
Game of Life Rules and Games Linh Tran ECE 573. What is Life? Life is just one example of a cellular automaton, which is any system in which rules are.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M
1 The Game of Life Supplement 2. 2 Background The Game of Life was devised by the British mathematician John Horton Conway in More sophisticated.
CELLULAR AUTOMATON Presented by Rajini Singh.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 9 Introduction to Arrays
CS305j Introduction to Computing Two Dimensional Arrays 1 Topic 22 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right.
Project 1CS-4513, D-Term Programming Project #1 Concurrent Game of Life Due Friday, March 20.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
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;
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Lists in Python.
More Accurate Rate Estimation CS 170: Computing for the Sciences and Mathematics.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies.
LANGUAGE AND TOOLS GURU S UHAN CANARAN PROJECT MANAGER A NDREAS NILSSON SYSTEM ARCHITECT A KSHAT SIKARWAR SYSTEM INTEGRATOR E RIC SCHMIDT TESTER AND VALIDATOR.
The Game of Life Erik Amelia Amy. What is the “Game of Life?” The “Game of Life” (often referred to as Life) is not your typical game. There are no actual.
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
A Variation on Conway’s Game of Life Winston Lee EPS 109.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
David Squeri CELLULAR AUTOMATON. A cellular automaton is an array of cells that switch on or off depending on whether other cells are on or off. “Rules”
Ionut Trestian Northwestern University
Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ARRAYS Multidimensional realities Image courtesy of
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.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Two-Dimensional Arrays
Topic Dimensional Arrays
Two-Dimensional Arrays
Topic 26 Two Dimensional Arrays
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
2D Array and Matrix.
Lecture 13: Two-Dimensional Arrays
Multidimensional Arrays
Multi-Dimensional Arrays
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

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 column row 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? 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 mind 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  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()); A B C D. 5 8 then a runtime error occurs E.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   

Generation *. *. * *. * * * *.. * *. *. * * *. * * indicates occupied,. indicates empty

Or

Generation *. *. *..... *. *. *.. * indicates occupied,. indicates empty

Or, Generation

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

Problem  Implement a program to run the simulation