Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to C Programming
Arrays.
Lesson Four: More of the Same
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Week 6: Arrays 1.  Loops are great  But, without a way to talk about a group of values, we can’t get the full potential out of a loop  Enter: the array.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
How to Create a Videogame By: Connor McCann. Java Java is one of many programming languages Java is used to run web browsers and most PC video games I.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Computer Science 210 Computer Organization Arrays.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
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}};
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.
Arrays.
Arrays.
Week 9 - Friday.  What did we talk about last time?  Static method practice  Finished the Game of Life.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
COMP More About Arrays Yi Hong June 05, 2015.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Multidimensional Arrays Computer and Programming.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
COMP 110 More arrays, 2D arrays, Program 4
Week 9 - Monday CS 121.
Two-Dimensional Arrays
Lesson 2: Building Blocks of Programming
Topic 26 Two Dimensional Arrays
Multidimensional Arrays
CS2011 Introduction to Programming I Methods (II)
Suggested self-checks: Section 7.11 #1-11
Week 7 - Monday CS 121.
Presentation transcript:

Week 9 - Wednesday

 What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life

 To declare a two dimensional array, we just use two sets of square brackets ( [][] ):  Doing so creates a variable that can hold a 2D array of int s  As before, we still need to instantiate the array to have a specific size: int [][] table; table = new int[5][10];

 You have to index into 2D arrays twice in order to get an element  Think of the first index as which row and the second as which column int [][] table = new int[5][10]; table[4][7] = 3; int x = table[4][7] + 5; int [][] table = new int[5][10]; table[4][7] = 3; int x = table[4][7] + 5;

 A cell is represented by a block in a grid  Each cell has 8 neighbors  Simple rules for a cell “coming to life” or “dying”: 1. A live cell with fewer than 2 live neighbors dies from loneliness 2. A live cell with more than 3 live neighbors dies from overcrowding 3. A live cell with exactly 2 or 3 neighbors keeps living 4. A dead cell with exactly 3 living neighbors comes to life

 We can represent the grid of cells with a 2D array of boolean values  true means alive  false means dead  Each iteration, we draw the grid onto the screen with StdDraw  Black means alive  White means dead  Then, we update the grid to contain the new values  The grid stores the state of the game  We still have to use StdDraw to draw that state

 It doesn’t have to stop at 2 dimensions!  You can have 3 or more  Here’s an example with 3 dimensions:  Unfortunately, the font gets small int[][][] rubiksCube = new int[3][3][3]; int count = 1; for( int i = 0; i < 3; i++ ) for( int j = 0; j < 3; j++ ) for( int k = 0; k < 3; k++ ) { rubiksCube[i][j][k] = count; count++; } int[][][] rubiksCube = new int[3][3][3]; int count = 1; for( int i = 0; i < 3; i++ ) for( int j = 0; j < 3; j++ ) for( int k = 0; k < 3; k++ ) { rubiksCube[i][j][k] = count; count++; }

 It looks like whatever you want it to  You can visualize it in 3D if you want  There are other techniques  It’s just a way to store data  It doesn’t actually look like anything inside the computer

 Sometimes you have data categorized in several different ways  For example, E-Town might keep some statistics according to Year, Gender, and Race  0 – Freshman  1 – Sophomore  2 – Junior  3 – Senior  Perfect candidate for a 3D array  0 – Male  1 – Female  0 – African American  1 – Asian  2 – Caucasian  3 – Other

 Too many brackets  Too much stuff  Total size used is the product of the length of all the dimensions  100 x 100 x 100 = 1,000,000  Hard to visualize, hard to imagine  Up as high as 4 is sometimes useful  Don’t go beyond 2 on a regular basis

 By now, everyone should be familiar with the simple method that returns the maximum of two values: public static int max( int a, int b ) { if( a > b ) return a; else return b; } public static int max( int a, int b ) { if( a > b ) return a; else return b; }

 What if we wanted to have a method with the same name that took three arguments and gave the maximum of them? public static int max( int a, int b, int c ) { if( a > b && a > c ) return a; else if( b > a && b > c ) return b; else return c; } public static int max( int a, int b, int c ) { if( a > b && a > c ) return a; else if( b > a && b > c ) return b; else return c; }

 Yes!  All that we need is the parameters to be different so that the compiler can figure out which function to use  The number or types (or both) of the parameters must be different  A different return type is not enough!

 We can even call one overloaded function from another: public static int max(int a, int b, int c) { return max( max(a,b), c ); } public static int max(int a, int b, int c) { return max( max(a,b), c ); }

 Consider the following two methods: Are they legally overloaded methods? public static int flibble( int a, double b ) { return 10; } public static int flibble( double a, int b ) { return 20; } public static int flibble( int a, double b ) { return 10; } public static int flibble( double a, int b ) { return 20; }

 But, which one gets called if I have a line of code that says: int x; x = flibble( 4, 5 ); No one knows!

 More method practice  Lab 9

 Keep reading Chapter 8 of the textbook  Keep working on Project 3  Due this Friday