Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two-Dimension Arrays Computer Programming 2.

Similar presentations


Presentation on theme: "Two-Dimension Arrays Computer Programming 2."— Presentation transcript:

1 Two-Dimension Arrays Computer Programming 2

2 Objective/Essential Standard
Essential Standard 3.00 Apply Advanced Properties of Arrays Essential Indicator 3.01 Apply procedures to construct two-dimensional arrays. (6%)

3 Two-Dimensional Arrays
Two-Dimensional Array = Rectangular Array A two-dimensional array contains rows and columns. This type of array requires two indices, one for the row and one for the column

4 Two-Dimensional Arrays
Declaring a two-dimensional array dataType [ , ] name = new dataType [r, c]; where r = the number of rows and c = the number of columns. Example int [ , ] intArray = new int[3, 3]; This would declare a 3 by 3 array. 1 2 1 2

5 Declaring & Initializing a Two-Dimensional Array
Example 1 Declare with an initializer list int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; string[,] friends = new string[2, 2] { {"Mike","Anna"}, {"Mary",“John"} }; Example 2 You can omit the size of the array with an initializer list. int[,] numbers = new int[ , ] { {1, 2}, {3, 4}, {5, 6} }; string[,] friends = new string[ , ] { {"Mike","Anna"}, {"Mary",“John"} }; Example 3 You can omit the size of the array and the new operator if you provide the initializer list. int[,] numbers = int[ , ] { {1, 2}, {3, 4}, {5, 6} }; string[,] friends = string[ , ] { {"Mike","Anna"}, {"Mary",“John"} };

6 Adding Values Individually
You can add values into your array as follows. intArray[r, c] = value; Example: intArray[1, 1] = 3; 1 2 3 1 2

7 Accessing Values in a Two-Dimensional Array
Nested For Loops give you more control. Use GetLength() to return how many elements are in that “Dimension”. O is Dimension 1 (rows) 1 is Dimension 2 (columns) int[,] intArr= new int[ , ] {{9, 99}, {3, 33}, {5, 55}}; for (int r = 0; r < intArr.GetLength(0); r++) { for (int c = 0; c < intArr.GetLength(1); c++) { Statements; } }

8 Creating a Grid In XNA you can create a grid of rectangles that you can use to mimic a gameboard. Declaring the two-dimensional array of rectangles. Rectangle [ , ] grid = new Rectangle[2, 2];

9 Load the Rectangles into the Grid
for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) grid[i, j] = new Rectangle(x, y, 50, 50); x += 50; //change the x location } y += 50; //move y location down x = 0; //reset the x

10 Nested For Loops The Nested For Loop can be used in:
The Initialize method to load your images picArray[i, j] = Content.Load<Texture2D>(“image"); The Draw method to “draw” your images SpriteBatch.Draw(picArray[i,j], grid[i,j], Color.White);

11 Check Your Understanding
Declare an array called nums that will hold 5 integers. int[] nums = new int[5]; What are the index numbers of the num array? 0, 1, 2, 3, 4 Given the nums array just declared, how would you write the for loop to traverse it? for (int i = 0; i < 5; i++)

12 Check Your Understanding
Declare the table array represented below. int [,] table = new int {{8,7}, {6,5}, {4,3}}; Given the table array, what would be the result of the following statement? Table[3,2] = 9; Code won’t compiler because the index is out of bounds. 8 7 6 5 4 3

13 Check Your Understanding
Write the for loop to iterate through the 2D array called matrix. for (int i = 0; i < matrix.GetLength(0); i++) for (int j = 0; j < matrix.GetLength(1); j++) What code would be inside the above nested loop if you wanted to create a running total? sum += matrix[i, j];

14 More Information For more information about the Array class:


Download ppt "Two-Dimension Arrays Computer Programming 2."

Similar presentations


Ads by Google