SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding 2D Arrays: Creation, Modification, and Iteration

taffy avatar
16 diapositives · 301 Views · Jul 31, 2014

Learn the basics of 2D arrays, creating, modifying, and iterating through them with examples in different data types. Understand the structure, indexing, and usage of 2D arrays for efficient data manipulation. Dive into examples and practice to enhance your skills in working with 2D arrays.

Understanding 2D Arrays: Creation, Modification, and Iteration - slide 1
01 / 16Understanding 2D Arrays: Creation, Modification, and Iteration
1 / 17

Télécharger la présentation

Understanding 2D Arrays: Creation, Modification, and Iteration
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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?