Title: Homework 9
1Homework 9
Sun., 11/3
( M T sections )
at midnight
Mon., 11/4
( W Th sections )
http//www.cs.hmc.edu/courses/2001/fall/cs5/week_0
9/homework.html
names and hours on syllabus on CS 5 home
page http//www.cs.hmc.edu/courses/2002/fall/cs5/
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)
2Problem 2 -- Life
Evolutionary rules
- Everything depends on ones eight neighbors
- 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)
white cells are empty
black cells are alive
3Problem 2 -- Life
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
white cells are empty
black cells are alive
4Problem 2 -- Life
// implementing the evolutionary rules
r,c
last
r,c
next
5Problem 2 -- Life
public void update(int last, int next)
// outline of code
g.lifeUpdate(next) //draw new state!
6Problem 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).
7Problem 1
Overall structure of main similar to HW8s
problem 1
Initial Setup
Get the number of rows and columns
Create an array of the appropriate number of
elements (doubles).
Get initial values from the user into the array.
printMenu enterArray printArray multRow addR1ToR2
addMR1ToR2 solve
Menu
Methods
(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?
82d arrays -- naming things
double arr
arr new doublenRowsnCols
double
double
double
double
double
double
double
double
double
double
double
double
9Multiplying rows
Before
After
2.00 3.00 -1.00 -8.00 -3.00 -1.00
2.00 42.00 1.00 -9.00 4.00 56.00
2.00 3.00 -1.00 -8.00 -3.00 -1.00
2.00 42.00 10.00 -90.00 40.00 560.00
public static void multRow(double arr, int r,
double m)
10Gaussian Elimination
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
Will any array work?
arr
Goal
- get 1s along the diagonal
- get 0s elsewhere on the left
Algorithm
2.00 3.00 -1.00 -8.00 -3.00
-1.00 2.00 42.00 1.00
-9.00 4.00 56.00
- how many columns?
- how many rows in each column?
11Solve
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...
12Solve
public static void solve(double arr)
13Debugging Aids
//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 lt
arr.length i) double blah
1/arrii multRow(arr,i,blah) if(debug)
H.out.print(blah ") H.out.print(blah)
H.out.println(".........") printArra
y(arr) userPrompt() //do some more
stuff //some more stuff