Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 9 - Monday CS 121.

Similar presentations


Presentation on theme: "Week 9 - Monday CS 121."— Presentation transcript:

1 Week 9 - Monday CS 121

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

3 Questions?

4 Project 3

5 2D Arrays

6 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

7 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];

8 Visualization of 2D arrays
Like matrices, we usually visualize the first dimension as the rows and the second dimension as the columns Second Dimension 1 2 3 4 First Dimension

9 Visualization of 2D arrays
Let’s write a little code to put data into the table int [][] 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++; }

10 Visualization of 2D arrays
The result of that code is: Second Dimension 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 First Dimension

11 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

12 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

13 Algorithm for chess problem
Find the row and column location of both the queen and the pawn The pawn is in danger if: The queen and the pawn have the same row The queen and the pawn have the same column If the absolute value of the differences between their rows and the absolute value of the differences between their columns are the same

14 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”: A live cell with fewer than 2 live neighbors dies from loneliness A live cell with more than 3 live neighbors dies from overcrowding A live cell with exactly 2 or 3 neighbors keeps living A dead cell with exactly 3 neighbors comes to life

15 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

16 3D and Higher Dimension Arrays

17 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++; }

18 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 the computer

19 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

20 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

21 Upcoming

22 Next time… Finish Game of Life Overloading methods
More method practice

23 Reminders Keep reading Chapter 8 of the textbook
Keep working on Project 3 Due this Friday


Download ppt "Week 9 - Monday CS 121."

Similar presentations


Ads by Google