Download presentation
Presentation is loading. Please wait.
Published byIrwan Makmur Modified over 6 years ago
1
2D Arrays Just another data structure Typically use a nested loop
A “grid” Similar to 1D arrays (indices, static, contiguous) Add another dimension Hold 2D terrain Typically use a nested loop Find min/max/sum/average Print out each element
2
int[,] grid = new int[4, 4];
3
int[,] grid = new int[4, 4]; <data type>[,] <name> = new <data type>[WIDTH, HEIGHT];
4
int[,] grid = new int[4, 4]; 4 4
5
int[,] grid = new int[4, 4]; How do we initialize this?
6
for (int y = 0; y < 4; y++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { } y
7
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y
8
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y y
9
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 y
10
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 y
11
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 y
12
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 y
13
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 y
14
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 y
15
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 y
16
false! int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } false! x y 4
17
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } y 1 y
18
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 y
19
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 y
20
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 1 y
21
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 1 y 1
22
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 1 y 1
23
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 1 y 1 2
24
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 1 y 1 2
25
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 1 y 1 2 3
26
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 1 y 1 2 3
27
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 1 y 1 2 3
28
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } y 2 1 2 3 y
29
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 1 2 3 y
30
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 1 2 3 y
31
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 2 1 2 3 y
32
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 2 1 2 3 y 2
33
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 2 1 2 3 y 2
34
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 2 1 2 3 y 2 4
35
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 2 1 2 3 y 2 4
36
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 2 1 2 3 y 2 4 6
37
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 2 1 2 3 y 2 4 6
38
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 2 1 2 3 y 2 4 6
39
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } y 3 1 2 3 2 4 6 y
40
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 1 2 3 2 4 6 y
41
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 1 2 3 2 4 6 y
42
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 3 1 2 3 2 4 6 y
43
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 1 3 1 2 3 2 4 6 y 3
44
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 3 1 2 3 2 4 6 y 3
45
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 2 3 1 2 3 2 4 6 y 3 6
46
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 3 1 2 3 2 4 6 y 3 6
47
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x x y 3 3 1 2 3 2 4 6 y 3 6 9
48
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 3 1 2 3 2 4 6 y 3 6 9
49
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } x y 4 3 1 2 3 2 4 6 y 3 6 9
50
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) {
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } y 4 1 2 3 2 4 6 3 6 9
51
false! int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } false! y 4 1 2 3 2 4 6 3 6 9
52
int[,] grid = new int[4, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { grid[x, y] = x*y; } // Continue processing 1 2 3 2 4 6 3 6 9
53
Finding the Smallest int smallest = grid[0,0];
for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (smallest > grid[x, y]) { smallest = grid[x, y]; }
54
Finding the Sum int sum = 0; for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) { sum += grid[x, y]; }
55
Finding the Average int sum = 0; for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) { sum += grid[x, y]; } double average = sum/16.0f;
56
Things to Note All of these examples had a nested loop
Only the logic changed! 2D arrays are similar to 1D arrays
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.