Week 9 - Monday. What did we talk about last time? Method practice Lab 8

Preview:

Citation preview

CS 121Week 9 - Monday

Last time

What did we talk about last time? Method practice Lab 8

Questions?

Project 3

2D Arrays

2D arrays

Just as it is possible to make a one dimensional list out of a single data type, it is also possible to make a table out of one data type

We can extend the arrays you know to have two dimensions with very similar syntax

Declaration

To declare a two dimensional array, we just use two sets of square brackets ([][]):

Doing so creates a variable that can hold a 2D array of ints

As before, we still need to instantiate the array to have a specific size:

int [][] table;

table = new int[5][10];

Visualization of 2D arrays Like matrices, we usually visualize

the first dimension as the rows and the second dimension as the columns

0 1 2 3 4 5 6 7 8 90 1234

Second Dimension

Firs

t D

imen

sion

Visualization of 2D arrays Let’s write a little code to put data

into the tableint [][] table = new int[5][10];int label = 1;

for( int i = 0; i < 5; i++ )for( int j = 0; j < 10; j++ ){table[i][j] = label;label++;}

Visualization of 2D arrays The result of that code is:

1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 50

0 1 2 3 4 5 6 7 8 90 1234

Second Dimension

Firs

t D

imen

sion

Chessboard We could represent a chessboard as an 8 x 8

array of chars Use the following encoding:

'P' = pawn 'N' = knight 'B' = bishop 'R' = rook 'Q' = queen 'K' = king

Use upper case characters for black pieces and lower case characters for white ones

Checking a pawn for danger Imagine there is a pawn randomly set on the

board and a queen of the opposite color on the board

Write a program to see if the queen can capture the pawn in the next move

Q

p

Algorithm for chess problem Find the row and column location of

both the queen and the pawn The pawn is in danger if:

1. The queen and the pawn have the same row

2. The queen and the pawn have the same column

3. If the absolute value of the differences between their rows and the absolute value of the differences between their columns are the same

Conway’s Game of Life A cell is represented by a block in a grid Each cell has 8 neighbors Simple rules for a cell “coming to life” or

“dying”:1. A live cell with fewer than 2 live neighbors dies

from loneliness2. A live cell with more than 3 live neighbors dies

from overcrowding3. A live cell with exactly 2 or 3 neighbors keeps

living4. A dead cell with exactly 3 neighbors comes to life

Implementing Conway's Game of Life We can represent the grid of cells with a 2D

array of boolean values true means alive false means dead

Each iteration, we draw the grid onto the screen with StdDraw Black means alive White means dead

Then, we update the grid to contain the new values

The grid stores the state of the game We still have to use StdDraw to draw that state

3D and Higher Dimension Arrays

4th dimensional rocketships going up

It doesn’t have to stop at 2 dimensions! You can have 3 or more Here’s an example with 3 dimensions:int[][][] rubiksCube = new int[3][3][3];int count = 1;

for( int i = 0; i < 3; i++ )for( int j = 0; j < 3; j++ )for( int k = 0; k < 3; k++ ) {rubiksCube[i][j][k] = count;count++;}

What does a 3D array look like? It looks like whatever you want it to You can visualize it in 3D if you want There are other techniques It’s just a way to store data It doesn’t actually look

like anything inside thecomputer

Why use a high dimensional array? Sometimes you have data

categorized in several different ways For example, E-town might keep

some statistics according to Year, Gender, and Race 0 – Freshman 1 – Sophomore 2 – Junior 3 – Senior

Perfect candidate for a 3D array

0 – Male 1 – Female

0 – African American

1 – Asian 2 – Caucasian 3 – Other

Why not to use a high dimensional array Too many brackets Too much stuff

Total size used is the product of the length of all the dimensions

100 x 100 x 100 = 1,000,000 Hard to visualize, hard to imagine Up as high as 4 is sometimes useful Don’t go beyond 2 on a regular basis

Upcoming

Next time…

Finish Game of Life Overloading methods More method practice

Reminders

Keep reading Chapter 8 of the textbook

Keep working on Project 3 Due this Friday

Recommended