CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue.

Slides:



Advertisements
Similar presentations
IST380 Loops got you going in circles? Nest them!
Advertisements

Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Computer Programming w/ Eng. Applications
Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
Mock test review Revision of Activity Diagrams for Loops,
Computer Science 1620 Loops.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Lecture 16: Big-Oh Notation
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Binary Arithmetic Math For Computers.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
What's ahead? Conway gives a whole new outlook on "loopy" ! John Conway 10/18 no class - Fall break 10/19 no new hw due 10/11 Dynamic & binary data 10/12.
1 iSee Player Tutorial Using the Forest Biomass Accumulation Model as an Example ( Tutorial Developed by: (
The not-so-subtle art of singling out the best (and worst) of anything… Computing with language Computing to the max You know this would make me hungry…
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
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.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
This week in CS 5 HW 9 (2 problems) M/T sections W/Th sections due Sunday, 11/4 at midnight due Monday, 11/5 at midnight Recitation for HW9 -- Friday 11/2.
Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems
Vectors and Grids Eric Roberts CS 106B April 8, 2009.
CSC 107 – Programming For Science. The Week’s Goal.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Copyright © 2011 Pearson Education, Inc. Solving Linear Systems Using Matrices Section 6.1 Matrices and Determinants.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Ionut Trestian Northwestern University
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
When the program is first started a wizard will start to setup your Lemming App. Enter your company name and owner in the fields designated “Company Name”
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Math 1320 Chapter 3: Systems of Linear Equations and Matrices 3.2 Using Matrices to Solve Systems of Equations.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
EECS 110: Lec 12: Mutable Data
User-Written Functions
EGR 2261 Unit 10 Two-dimensional Arrays
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Two-Dimensional Arrays
CSC 113 Tutorial QUIZ I.
More Loop Examples Functions and Parameters
HMC’s Galileo Auditorium Some of Cope’s generated MP3s:
Ionut Trestian Northwestern University
More 2 D Array.
2D Array and Matrix.
Two-Dimensional Arrays
Northwestern University
EECS 110: Lec 12: Mutable Data
More 2D Array and Loop Examples Functions and Parameters
2D Array and Matrix Application: Game of Life
Presentation transcript:

CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue. 11/16-17 Caffeine Anyone choose Gauss or Conway for Halloween? Coming up in CS 5:

Mandelbrot sets… !

Mutable vs. Immutable data Mutable types: dictionary Immutable types: list tuple string int float bool What's a dictionary? I guess I'll have to look it up! s 'hi' s = 'hi' you can change what is named, but you can't change already-named data!

Reference vs. Value dictionary list tuple string int float bool L L[0]L[1]L[2] Reference, Pointer, id L = [5,42,'hi'] 5 42 'hi' s s = 'hi' Whee! Mutable types:Immutable types:

Pass By Value def main() """ calls conform """ print " Welcome to Conformity, Inc. " fav = 7 conform(fav) print " My favorite number is", fav def conform(fav) """ sets input to 42 """ fav = 42 return fav 7 fav

7 Pass By Value def main() """ calls conform """ print " Welcome to Conformity, Inc. " fav = 7 conform(fav) print " My favorite number is", fav def conform(fav) """ sets input to 42 """ fav = 42 return fav 7 fav PASS BY VALUE “Pass by value” means that data is copied when sent to a function 42

Passing lists by value… def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 What gets passed by value here? fav L[0]L[1] 7 11 fav

Passing lists by value… def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 L[0]L[1] 7 11 can change data elsewhere! The reference is copied! 42 fav

Watch out! You can change the contents of lists in functions that take those lists as input. Those changes will be visible everywhere. (actually, lists or any mutable objects) (immutable objects are safe, however)

Views of the world Where does CS fit ?

Views of the world Engineers think their equations approximate reality.

Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations.

Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care.

Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care. Axioms Definitions Creating structure from a few simple facts... Proof 60˚ 70˚ 20˚ 10˚ ?

60˚70˚ 20˚ 10˚ ? (without using trig) The John Conway Challenge….

Views of the world Axioms Definitions Creating structure from a few simple facts... Creating structure from a few simple actions... if/else while for arithmetic operations variables arrays ProofAlgorithm CSMathematics

Views of the world Axioms Definitions Creating structure from a few simple facts... Creating structure from a few simple actions... if/else while for arithmetic operations variables arrays ProofAlgorithm Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care. CS

Lists’ flexibility Lists can hold ANY type of data A = [ 42., 75., 70. ] float list A they don’t have to be horizontal lists!

double list A they don’t have to be horizontal lists! Lists’ flexibility Lists can hold ANY type of data A = [ 42., 75., 70. ] float list A

Lists’ flexibility Lists can hold ANY type of data double list A int list A “go” “red” “sox!” String list A

2d lists or arrays Lists can hold ANY type of data -- including lists ! list A A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

list A 2d arrays list A[0] A[1] A[2] Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]

list A Jagged arrays list A[0] A[1] A[2] Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] Rows within 2d arrays need not be the same length…

list A A[0] A[1] A[2] A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] Rows within 2d arrays need not be the same length… No "jagged" arrays… at least not in hw 9

Rectangular arrays list A A[0] A[1] A[2] How many rows does A have, in general ? How many columns does A have, in general ? What does each component of A[1][2] mean ? A[1][2] = 42 A[2][3] A[0][0]

Sizing up arrays… How could we create this rectangular array of 0 s? or A = 5*[ 3*[0] ] A = 3*[ 5*[0] ] [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]

Sizing it up… How could we create a rectangular array (of default data, 0 ), given its height and width ? but NEITHER ONE works! A = height*[ [0]*width ] A = width*[ [0]*height ] this is the right size, but doesn't work! because lists are handled by reference !

What's really going on? A = 3*[ 5*[0] ] inner = 5*[0] A = 3*[inner] Inner esting! list A inner copies the list reference, not the list data "shallow copy"

Safely creating arrays… def createOneRow( width, height ): """ does just that """ row = [] # start with nothing for col in range( width ): return row So, how would you create a list of rows!?

Problem 2 Menu An array of array handlers... Functions printMenu enterValues print multRow addRowSIntoRowD addMofRowSIntoRowD solve (1) Enter the size and values of an array (2) Print the array (3) Multiply an array row by a constant (4) Add one row into another (5) Add a multiple of one row to another (6) Solve! (9) Quit Which choice would you like? S ~ source row index D ~ destination row index M ~ a multiplier

“Quiz” def mystery(A): """ what happens to A ? """ NUM_ROWS = len(A) NUM_COLS = len(A[0]) for row in range( 0,NUM_ROWS ): for col in range( 0,NUM_COLS ): if row == col: A[row][col] = 42 else: A[row][col] += Before After A A Starting with the 2d array A shown here, what are the values in A after running this code? row 0 row 1 row 2 col 0 col 1col 2col 3 What are the resulting values in A? Name(s)

“Quiz” def add2ofRow1IntoRow2( A ): before after A A two of row 1 are to be added to row 2 row 0 row 1 row 2 row 0 row 1 row 2 Write a method that adds two times the values in row # 1 into the values in row # 2. Only row 2's values change. You may assume that A has at least three rows! How could you make the source and destination rows inputs to this function - and then use those inputs? The values in row 1 should not change

def addRowSIntoRowD( s, d, A ): row 0 row 1 row 2 row 0 row 1 row 2 Write a method that adds row # s into the values in row # d. s d s d How about a multiple, m, of the source row? s d ~ index of source row ~ index of destination row

Problem 2 Menu An array of array handlers... (1) Enter the size and values of an array (2) Print the array (3) Multiply an array row by a constant (4) Add one row into another (5) Add a multiple of one row to another (6) Solve! (9) Quit Which choice would you like? Functions printMenu enterValues print multRow addRowSIntoRowD addMofRowSIntoRowD solve

Gaussian Elimination 2p + 3n + -1q = p + -1n + 2q = p + -9n + 4q = Goal: Find p,n,q Carl Gauss is so money!

Gaussian Elimination 2p + 3n + -1q = p + -1n + 2q = p + -9n + 4q = Goal: get 1s along the diagonal get 0s elsewhere on the left Find p,n,q 1p + 0n + 0q = p + 1n + 0q = p + 0n + 1q = 25.00

Gaussian Elimination 2p + 3n + -1q = p + -1n + 2q = p + -9n + 4q = Goal: get 1s along the diagonal get 0s elsewhere on the left Find p,n,q 1p + 0n + 0q = p + 1n + 0q = p + 0n + 1q = Just the array is necessary ! We can get rid of the variables... Using only row operations (our methods) ! where to start?

Solve multiply Row 0 by 0.5 Which direction should we head next !?! GOAL: to get this side to look like the identity matrix. RESULT: this column will be our solution!

Solve multiply Row 0 by 0.5 a hint as to the direction to head next… !

Solve multiply Row 0 by add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2

Solve multiply Row 0 by multiply Row 1 by 1/3.5 add a multiple of Row 1 to Row 0 add a multiple of Row 1 to Row 2 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2

Solve multiply Row 0 by add 3 times Row 0 to Row 1 add -1 times Row 0 to Row same for other columns as far as possible... multiply Row 1 by 1/3.5 add a multiple of Row 1 to Row 0 add a multiple of Row 1 to Row 2

Lab Problem -- “Life” Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead) John Conway

Problem 1 -- Life Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead)

Problem 1 -- Life Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead)

Problem 1 -- Life Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead) life out there... Keep going!

Problem 1 -- Creating Life updateNextLife( oldB, newB ) old generation or "board" new generation or "board"

Problem 1 -- Details For each generation… 0 represents an empty cell 1 represents a living cell outermost edge should always be left empty (even if there are 3 neighbors) compute all cells based on their previous neighbors before updating any of them updateNextLife( oldB, newB ) old generation or "board" new generation or "board" life out there... ?

Problem 1 -- to  and beyond! Are there stable life configurations? Are there oscillating life configurations? Are there self-propagating life configurations? "rocks" "plants" "animals" period 3 period 2

Problem 1 -- to  and beyond! Are there life configurations that expand forever? What is the largest amount of the life universe that can be filled with cells? How sophisticated can the structures in the life universe be? Are all feasible configurations reachable? "glider" "Gosper glider gun"

Today you'll be Lifing it up in lab! on over…so

Problem 1 -- to  and beyond! public static void update(int[][] last, int[][] next) { for (int r=1 ; r<last.length-1 ; ++r) { for (int c=1 ; c<last[r].length-1 ; ++c) { int oldvalue = last[r][c]; if (oldvalue == 0) // look at last next[r][c] = 1; // assign to next else next[r][c] = 0; } What will this do?

Problem 2 -- Details public static void update(int[][] last, int[][] next) { for (int r=1 ; r<last.length-1 ; ++r) { for (int c=1 ; c<last[r].length-1 ; ++c) { int oldvalue = last[r-1][c-1]; if (oldvalue == 0) next[r][c] = 1; else next[r][c] = 0; } How does this change things?

Problem 2 – Multi-species Life (!) Create a set of rules to evolve two species of cells (plus empty). The Challenge updateMulti(int[][] last, int[][] next) Give both species a good chance of survival in the same environment. stability is an open biological question… more species are OK, too… …

Lab this week Problem 1: Gaussian Elimination You’ll need to write (and use) Problem 2: Life and Multi-species Life! Extra Credit: printMenu enterValues print multRow addRowaToRowb addMxRowaToRowb solve public void update(int[][] last, int[][] next) A matrix-inverse feature for Problem 1... Doughnut Life for Problem 2 … Lab: M-Z public void updateMulti(int[][] last, int[][] next)

“Quiz” public static void mysteryMethod(int[][] A) { for (int r = 0 ; r < A.length ; ++r) { for (int c = 0 ; c < A[r].length ; ++c) { if (r == c) { A[r][c] = 42; } else { A[r][c] = A[r][c] + 1; } } // end of mystery method Before After A A Starting with the 2d array A shown here, what are the values in A after running this code? row 0 row 1 row 2 col 0 col 1col 2col 3 What are the resulting values in A?

“Quiz” public static void addTwoOfRow1IntoRow2(double[][] A) { } before after A A two of row 1 are to be added to row 2 example row 0 row 1 row 2 row 0 row 1 row 2 Write a method that adds two times the values in row #1 into the values in row #2. The values in row #1 should not change! (The values in row #0 also don’t change!)

Watch out! public static void main(String[] args) { H.out.println(“Welcome to conformity, inc.”); int[] fav = new int[2]; fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s conform(fav); H.out.println(fav + “ and ” + fav + “! Me too!”); } int[] fav fav[0]fav[1] What will happen here?!

Watch out! public static void main(String[] args) { H.out.println(“Welcome to conformity, inc.”); int[] fav = new int[2]; fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s conform(fav); H.out.println(fav[0] + “ and ” + fav[1] + “! Me too!”); } public static void conform(int[] fav) { fav = new int[2]; fav[0] = 42; fav[1] = 42; return; } int[] fav fav[0]fav[1] int[] fav

This week in IS 313 HW 7 (2 problems) due Thursday, 11/7 at midnight A dizzying array of possibilities... Reading: Week 9’s online notes Still seeking a costume? John Conway Carl Gauss This week’s credits:

Survival of the stablest can they be unbounded spatially? what are stable “life” forms? The Questions of Life... can they grow forever? how fast can they travel? how densely can they grow?

“Pass By Value” public static void main(String[] args) { H.out.println(“Welcome to Conformity, Inc.”); int fav = 7; // The user’s favorite # conform(fav); H.out.println(fav + “ is my favorite, too!”); }

Design - not just software Please see instructions on HW 9. You have some choice on what parts of this chapter to read. Conceptual Models –Watch crown vs. buttons –Directory structures Visibility and Cues –Banks of glass doors –Telephone buttons Feedback – A refrigerator – Phone settings Don Norman's observations:

Affordances ~ physical and cultural expectations

Microsoft Access Windows NT Microsoft Outlook User interface Hall of Shame

Creating a 2d array def create2dArray( width, height ): """ does just that """ A = [] # start with nothing for row in range( height ): for col in range( width ): return A