1 / 16

2 Dimensional Arrays

2 Dimensional Arrays. Briana B. Morrison CSE 1302C Spring 2010. Topics. 2D vs 1D Creating 2D arrays Modifying Iterating Examples. 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

taffy
Download Presentation

2 Dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2 Dimensional Arrays Briana B. Morrison CSE 1302C Spring 2010

  2. Topics 2D vs 1D Creating 2D arrays Modifying Iterating Examples

  3. 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

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

  5. Creating 2D Arrays <data type>[,] <name> = new <data type> [<cols>,<rows>]; Notice The comma The columns and rows values

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

  7. Modifying a 2D Array 1 2 3 4 5 6 0 0 [1,0] [2,0] [3,0] [4,0] [5,0] [6,0] [0,0] myArray 1 [1,1] [2,1] [3,1] [4,1] [5,1] [6,1] [0,1] 2 [1,2] [2,2] [3,2] [4,2] [5,2] [6,2] [0,2] . . . 3 [1,3] [2,3] [3,3] [4,3] [5,3] [6,3] [0,3] 4 [1,4] [2,4] [3,4] [4,4] [5,4] [6,4] [0,4] 5 [1,5] [2,5] [3,5] [4,5] [5,5] [6,5] [0,5] [1,6] [2,6] [3,6] [4,6] [5,6] [6,6] [0,6] 6 myArray[2,3] = 42;

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

  9. Example 1 class Program { static void Main(string[] args) { #region First example for (inti = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { Console.Write((i * j).ToString( ) + " " ); } Console.WriteLine(); } #endregion

  10. Example 2 class Program { static int COLS = 10; static int ROWS = 10; static void Main(string[] args) { #region Second Example int[,] grid = new int[10, 20]; for (intcol = 0; col < 10; col++) for (int row = 0; row < 20; row++) grid[col, row] = col * row; PrintArray(grid); #endregion

  11. Example 2 (cont.) private static void PrintArray(int[,] A) { for (intcol = 0; col < 10; col++) { for (int row = 0; row < 20; row++) { Console.Write(A[col, row] + " "); } Console.WriteLine(); } }

  12. Battleship Example class Spot { public boolShipThere; public boolHasGuessed; } class Program { static int COLS = 10; static int ROWS = 10; static void Main(string[] args) { Spot[,] board = new Spot[COLS, ROWS]; for (inti = 0; i < COLS; i++) { for (int j = 0; j < ROWS; j++) { board[i, j] = new Spot(); } } PlaceShips(board);

  13. Battleship Example private static void PlaceShips(Spot[,] board) { board[1, 1].ShipThere = true; board[1, 2].ShipThere = true; board[1, 3].ShipThere = true; board[1, 4].ShipThere = true; board[5, 5].ShipThere = true; board[6, 5].ShipThere = true; board[7, 5].ShipThere = true; board[8, 5].ShipThere = true; board[9, 5].ShipThere = true; }

  14. Battleship Example char[ ] delimiters = { ' ' }; string input; do { PrintBoard(board); Console.Write("> "); input = Console.ReadLine(); if (input != "Q") { string[ ] tokens = input.Split(delimiters); int x = Int32.Parse(tokens[0]); int y = Int32.Parse(tokens[1]); board[x, y].HasGuessed = true; } } while (input != "Q"); }

  15. Battleship Example private static void PrintBoard(Spot[,] board) { for (int row = 0; row < ROWS; row++) { for (intcol = 0; col < COLS; col++) { if (board[col, row].HasGuessed) { if (board[col, row].ShipThere) { Console.Write(" S "); } else { Console.Write(" . "); } } else { Console.Write(" - "); } } Console.WriteLine(); } }

  16. Questions?

More Related