CSE 214 Computer Science II (Spring 2015)

RECITATION 3 – Arrays

Objective:

  1. To work with Arrays in Java.

2.  Analysis of code fragments

1.[20 min] Write a Java method that repeatedly selects and removes a random entry from an array until the array holds no more entries. link:http://www3.cs.stonybrook.edu/~sael/teaching/cse214/rec/RandomArray.java

[Note java.lang.Math.random() method is used to generate random numbers]

2. [20 min] Write the order of complexity for the following code fragments using Big O notation

(a) i=1;                      (b)                                         (c)

sum = 0;                         for (i=1; i<=n; i++)             for (i=1; i<=n; i++)

while (i <= n)               for (j=1; j<=i; j++)               for (j=1; j<=i; j++)

{                                                   X++                           for (k=1; k<=j; k++)

j=1;                                                                                        X++  

while (j <= n) {                                             

sum = sum + i;                             

j = j + 1;                                     

 }

i = i +1;                          

}

3. [Practice Question] Implement and Monitor the state change of cells for Conway’s Game of Life is a simulation devised by John Horton Conway in 1970 as a “zero player” game. The steps are as follows: link:http://www3.cs.stonybrook.edu/~sael/teaching/cse214/rec/Life.java

(i) Start off with a 2 dimensional array of equal width and height

(ii) Randomly populate this array with true’s and false’s. true’s represent live creates and false’s represent empty space.

(iii) Next, enter into an loop and evaluate the a cell after each generation




Examples:


(a)

 

 

true

 

 

true

 

 

 

true

 

 

 

 

 

 

 

 

 

 

 

true

true

 

 

 

 

 

 

 

 

 








(b)

 

 

 

 

 

true

true

 

 

true

true

 

true

true

 

 

 

 

 

 

true

true

 

true

 

 

true

 

true

true