More 2 D Array.

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

Hand Crafting your own program By Eric Davis for CS103.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
LAB 10.
Computer Programming Lab(5).
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Chapter 5 Loops.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
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.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Arrays.
Building Java Programs
Spreadsheets.
Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Two-Dimensional Arrays
CS100J Lecture 19 Previous Lecture This Lecture
Sit-In Lab 1 Ob-CHESS-ion
Repetition-Counter control Loop
Case Study 2 – Marking a Multiple-choice Test
TK1114 Computer Programming
עקרנות תכנות מונחה עצמים
CS1100 Computational Engineering
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
Chapter 6 More Conditionals and Loops
Yong Choi School of Business CSU, Bakersfield
2D Arrays.
Board: Objects, Arrays and Pedagogy
Nested Loop Review and Two-Dimensional Arrays
Topic 26 Two Dimensional Arrays
Two-Dimensional Arrays
Truth tables: Ways to organize results of Boolean expressions.
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Truth tables: Ways to organize results of Boolean expressions.
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Suppose I want to add all the even integers from 1 to 100 (inclusive)
class PrintOnetoTen { public static void main(String args[]) {
Lecture 12: 2D Arrays AP Computer Science Principles
Truth tables: Ways to organize results of Boolean expressions.
Building Java Programs
Continue with Life, Magic and Catch -up
Building Java Programs
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Building Java Programs
Chapter 8 Multidimensional Arrays
Computer Architecture and Assembly Language
Array Review Selection Sort
22C:21 Problem 2.3 Solution Outline.
AP Java Learning Objectives
Random Numbers while loop
Methods/Functions.
Building Java Programs
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
2D Array and Matrix Application: Game of Life
A type is a collection of values
Presentation transcript:

More 2 D Array

Learning Objective Review reading a program that uses arrays Be able to send a 2D array to a method Be able to return a 2D array from a method Take this knowledge to model Life

Dry Run: Passing a 2D Array to a Method public class MatrixPassingDemo { /* computes the sum of left diagonal elments */ static int sumThing(int mat[][]) { int i, j, sum = 0; int rows = mat.length; int cols = mat[0].length; for ( i = 0; i < rows; i++ ) { for ( j = 0; j < cols; j++ ) { if ( i == j ) { sum += mat[i][j]; } return sum; public static void main(String args[]) { int mat[][] = { { 2, 3, 8, 4 }, { 5, 1, 7, 3 }, { 9, 2, 6, 8 }, { 1, 4, 5, 7 } }; int x = sumThing(mat); System.out.println(“The result is " + x); }

Returning a 2D Array from a Method public class MatrixReturnDemo { static void display(int mat[][]) { for ( int i = 0; i < mat.length; i++ ) { for ( int j = 0; j < mat[0].length; j++ ) { System.out.print(mat[i][j] + " "); } System.out.println(); static int[][] getNewMatrix(int mat[][]) { int rows = mat.length; int cols = mat[0].length; int aux_mat[][] = new int[rows][cols]; for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < cols; j++ ) { aux_mat[i][j] = mat[i][j] * 10; return aux_mat; Returning a 2D Array from a Method public static void main(String args[]) { int mat[][] = { { 2, 3, 8 }, { 5, 1, 7 }, { 9, 2, 6 } }; int new_mat[][] = getNewMatrix(mat); System.out.println("mat : "); display(mat); System.out.println("new_mat : "); display(new_mat); }

Program: Life The universe is a two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated". Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Patterns - Any live cell with fewer than two live neighbors dies, as if caused by under population. - Any live cell with two or three live neighbors lives on to the next generation. - Any live cell with more than three live neighbors dies, as if by overpopulation. - Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Your application of Life You determine the size of the universe Fill your universe: Examples Each cell has a 1 in N chance of being alive for the first generation. You select the value for N or have the user enter the value for N. Look up potential life patterns. Run the game for several generations. Show the universe after each generation Push: Break the program into Methods. neighbors(), showUniverse(), populate() Push: Determine a way to use Objects in your solution. Push: Have your universe ‘wrap’

Input Format There will be exactly 6 lines, each containing integers separated by spaces. Each integer will be between -9 and 9 inclusive. Output Format Print the 2D array and print the answer to this problem on a single line. Sample Input 1 1 1 0 0 0 0 1 0 0 0 0 0 0 2 4 4 0 0 0 0 2 0 0 0 0 1 2 4 0 Sample answer 19 Explanation The hourglass which has the largest sum is: 2 4 4 2 1 2 4 You are given a 2D array. An hourglass in an array is a portion shaped like this: a b c d e f g For example, if we create an hourglass using the number 1 within an array full of zeros, it may look like this: 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 Actually, there are many hourglasses in the array above. The three leftmost hourglasses are the following: 1 1 1 1 1 0 1 0 0 1 0 0 The sum of an hourglass is the sum of all the numbers within it. The sum for the hourglasses above are 7, 4, and 2, respectively. In this problem you have to print the largest sum among all the hourglasses in the array.