Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems

Similar presentations


Presentation on theme: "Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems"— Presentation transcript:

1 Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems http://www.cs.hmc.edu/courses/2001/fall/cs5/week_09/homework.html 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: http://www.cs.hmc.edu/courses/2002/ fall/cs5/

2 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)

3 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

4 Problem 2 -- Life // implementing the evolutionary rules r,c last r,c next

5 public void update(int[][] last, int[][] next) { Problem 2 -- Life // outline of code } g.lifeUpdate(next); //draw new state!

6 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).

7 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

8 2d arrays -- naming things double[][] arr; arr = new double[nRows][nCols]; double

9 Multiplying rows public static void multRow(double[][] arr, int r, double m) { } 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 BeforeAfter 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 10.00 -90.00 40.00 560.00

10 Gaussian Elimination Goal: get 1s along the diagonal get 0s elsewhere on the left 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 Algorithm: go column-by-column how many columns? how many rows in each column? Setup: 2.00 3.00 -1.00 -8.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 arr Will any array work?

11 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 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...

12 Solve public static void solve(double[][] arr)

13 //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…


Download ppt "Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems"

Similar presentations


Ads by Google