Homework 05: Arrays Solved

25.00 $ 12.50 $

Category:

Description

Prerequisites

  1. Arrays
    • Know how to declare, initialize and use multidimensional arrays.
  2. Repetition
    • Use loops to traverse through a multidimensional array.
  3. Scanner Libraries

 

Description

Arrays are data structures consisting of a collection of elements of the same type. Each element is accessible via 1 or more indices. In a one-dimensional array, each element can be accessed using one index. For example, arr[2]​ accesses the third element of the one-dimensional array called arr​ .​ In a two-dimensional array, each element can be accessed using two indices. For example, arr[2][1]​ accesses the element at the third row and the second column of a two-dimensional array called arr​ .​

 

To access all elements of an array, it is convenient to use for​ loops. For a one-dimensional array, the following will print all elements of the array called arr​ , one on each line:​

 

for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]);

}

 

And similarly for a two-dimensional array, one for​ loop for each dimension can be used:​

 

for (int i = 0; i < garage.length; i++) { System.out.println(“Row: ” + i);

for (int j = 0; j < garage[i].length; j++) { System.out.println(arr[i][j]);

}

System.out.println(); }

 

In this assignment you will be creating a simulation for Conway’s Game of Life. To see a simulation you can go to https://bitstorm.org/gameoflife/. The general rules are as follows:

 

Rules:

  • For a cell that is ‘alive’ or ‘populated’:
  • Any cell with fewer than two live neighbors dies Any cell with more than three live neighbors dies ● Any cell with two or three neighbors survives.
  • For a cell that is ’empty’ or ‘unpopulated’:
  • Each cell with three neighbors becomes populated.

 

An empty cell will be denoted with a 0 whereas a populated cell will be shown as a 1.

 

Visual Example

 

In these examples the black cells are live and the white cells are dead.

 

                              Generation 0                                 Generation 1​

 

Tasks

Task 1 – Create the Initial Grid  (10 Points)

  1. At the beginning of the program, ask the user to enter one value for the size of the square matrix

 

  1. Then create a two-dimensional array of ints​ to represent the grid . You can assume that the user will input a valid integer​ .​

 

  1. Have the user enter the strings that follow the form “0,1,0,1,1,0…,1” for each row.

 

  1. Print out Generation 0: followed by the 2D array

 

 

Enter number of rows/columns:

3

 

1,0,1

1,1,0

0,1,1

 

Generation 0:

1 0 1

1 1 0

0 1 1

Task 2 – Calculating Next Generation (85 Points)

Using the Conway’s Game of Life Rules listed above, create the next generation of cells.

//after one iteration Generation 0 becomes Generation 1

Generation 0:

  • 1 1
  • 0 0

0 1 0

 

Generation 1:

  • 1 0
  • 0 1

0 0 0

Task 3 – Looping  (5 Points)

  1. If the user inputs “q” or “Q”, then the program will terminate

 

  1. If the user inputs any character other than “q” or “Q”, the program will output the next generation of cells.

Example Execution

Enter number of rows/columns: 3

0,1,1

1,0,0

0,1,0

 

Generation 0:

0  1 1

1  0 0

0 1 0

n

Generation 1:

0  1 0

1  0 1

0 0 0

n

Generation 2:

0 1 0

0 1 0

0 0 0

n

Generation 3:

0 0 0

0 0 0

0 0 0

n

Generation 4:

0 0 0

0 0 0

0 0 0

q

Program Terminated

 

Submission

Items needed for submission

– GameOfLife.java