Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Similar presentations


Presentation on theme: "CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick."— Presentation transcript:

1 CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

2 CSE 1301 2D vs 1D Arrays Still hold several values of the same type (homogeneous) Still based on a slot number (the index number), but has two now Still have instant access Still Static

3 CSE 1301 What 2D arrays look like 0 123456 myArray 0 1 2 3 4 5 6 [6,0] [6,1][6,2][6,3][6,4][6,5][6,6] [5,0] [5,1][5,2][5,3][5,4][5,5][5,6] [4,0] [4,1][4,2][4,3][4,4][4,5][4,6] [3,0] [3,1][3,2][3,3][3,4][3,5][3,6] [2,0] [2,1][2,2][2,3][2,4][2,5][2,6] [1,0] [1,1][1,2][1,3][1,4][1,5][1,6] [0,0] [0,1][0,2][0,3][0,4][0,5][0,6]......

4 CSE 1301 Creating 2D Arrays [,] = new [, ]; Notice – The comma – The columns and rows values

5 CSE 1301 Examples An array of shorts: short[,] someArray = new short[50,5]; An array of floats: float[,] myArray = new float[25,10]; An array of booleans: bool[,] list = new bool[640,480]; An array of chars: char[,] characters = new char[2,2];

6 CSE 1301 Modifying a 2D Array 0 123456 myArray 0 1 2 3 4 5 6 [6,0] [6,1][6,2][6,3][6,4][6,5][6,6] [5,0] [5,1][5,2][5,3][5,4][5,5][5,6] [4,0] [4,1][4,2][4,3][4,4][4,5][4,6] [3,0] [3,1][3,2][3,3][3,4][3,5][3,6] [2,0] [2,1][2,2][2,3][2,4][2,5][2,6] [1,0] [1,1][1,2][1,3][1,4][1,5][1,6] [0,0] [0,1][0,2][0,3][0,4][0,5][0,6] myArray[2,3] = 42

7 CSE 1301 Iterating Through 2D Arrays Working with 2D arrays, you will use two loops (nested) Example: byte[,] myPic = new byte[480,640]; for (int x=0; x < 480; x++) { for (int y=0; y < 640; y++) { myPic[x,y] = 0; }

8 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; }

9 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 000 000 000 myPic

10 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 000 000 000 myPic 0 x

11 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 000 000 000 myPic 0 x

12 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 000 000 000 myPic 0 x 0 y

13 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 000 000 000 myPic 0 x 0 y

14 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 000 000 000 myPic 0 x 0 y What happens?

15 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 100 000 000 myPic 0 x 0 y

16 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 100 000 000 myPic 0 x 1 y

17 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 1 0 0 000 000 myPic 0 x 1 y

18 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 110 000 000 myPic 0 x 1 y

19 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 110 000 000 myPic 0 x 2 y

20 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 110 000 000 myPic 0 x 2 y

21 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 0 x 2 y

22 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 0 x 2 y

23 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 0 x 3 y

24 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 0 x 3 y

25 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 1 x

26 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 1 x

27 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 1 x 0 y

28 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 000 000 myPic 1 x 0 y

29 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 001 000 myPic 1 x 0 y

30 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 001 000 myPic 1 x 1 y

31 CSE 1301 Now skipping ahead a bit

32 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 110 myPic 2 x 1 y

33 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 110 myPic 2 x 2 y

34 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 110 myPic 2 x 2 y

35 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 111 myPic 2 x 2 y

36 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 111 myPic 2 x 3 y

37 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 111 myPic 2 x 3 y

38 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 111 myPic 3 x

39 CSE 1301 Line by Line (a smaller example) byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 111 myPic 3 x

40 CSE 1301 Done! byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 0 12 0 1 2 111 111 111 myPic...

41 CSE 1301 Summary 2D Arrays are similar to 1D arrays Usually requires nested loops


Download ppt "CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick."

Similar presentations


Ads by Google