Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems Tutors available Fri Afternoon 1-4pm (Parsons & LAC) Sat Afternoon 1-4pm (Parsons & LAC) Sun Afternoon 1-4pm (Parsons & LAC} Sun Evening 7-12pm (Parsons & LAC) Mon Evening 7-12pm (Parsons & LAC) names and hours on syllabus on CS 5 home page: fall/cs5/
Problem 2 -- Life Evolutionary rules Everything depends on one’s eight neighbors black 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)
public void update(int[][] last, int[][] next) For each generation… 0 represents an empty cell 1 represents a living cell outermost edge should always be empty (even if there are 3 neighbors) compute all cells based on their previous neighbors before updating any of them Problem 2 -- Life black cells are alive white cells are empty
Problem 2 -- Life // implementing the evolutionary rules r,c last r,c next
public void update(int[][] last, int[][] next) { Problem 2 -- Life // outline of code } g.lifeUpdate(next); //draw new state!
Problem 2 -- Life Details on getting and using the code... Download from CS 5 home page (first page, top link). Be sure to unzip resulting file. Using project: go into NewGrid folder and open NewGrid.mcp project file. The CS5App.java file contains an empty update method You can use any colors to actually show the live and empty cells (default: black is alive; white is dead). Testing is easiest on a small (e.g. 10x10) grid. Click on squares to toggle their state (helpful in debugging).
Problem 1 (0) Change the values in the array (1) Print the array (2) Multiply an array row (3) Add one row to another (4) Add a multiple of one row to another (5) Solve! (9) Quit Which choice would you like? Menu Overall structure of main : similar to HW8’s problem 1 Initial Setup Get the number of rows and columns Get initial values from the user into the array. Create an array of the appropriate number of elements ( doubles ). Methods printMenu enterArray printArray multRow addR1ToR2 addMR1ToR2 solve
2d arrays -- naming things double[][] arr; arr = new double[nRows][nCols]; double
Multiplying rows public static void multRow(double[][] arr, int r, double m) { } BeforeAfter
Gaussian Elimination Goal: get 1s along the diagonal get 0s elsewhere on the left Algorithm: go column-by-column how many columns? how many rows in each column? Setup: arr Will any array work?
Solve multiply Row 0 by add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 repeat for column 2 and column 3 and so on...
Solve public static void solve(double[][] arr)
//for debugging.... public static void userPrompt() { H.out.print("enter any text to continue..."); String line = H.in.nextWord(); } public static void someFunction(int [][] arr) { boolean debug = true; for(int i = 0; i < arr.length; i++) { double blah = 1/arr[i][i]; multRow(arr,i,blah); if(debug) { H.out.print(“blah "); H.out.print(blah); H.out.println(" "); printArray(arr); userPrompt(); } //do some more stuff } //some more stuff… } Debugging Aids…