Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.

Similar presentations


Presentation on theme: "Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7."— Presentation transcript:

1 Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7

2 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 Typically use nested loops to traverse array 8/29/2019 CSE 1321

3 What 2D Arrays look like . . . . . . 1 2 3 4 5 6 myArray 1 2 3 4 5 6
1 2 3 4 5 6 [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] myArray 1 [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] 2 [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] . . . 3 [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] 4 [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] 5 [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] 6 [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] . . . 8/29/2019 CSE 1321

4 Ps Defining a 2D array CREATE array nums [numRows][numColumns]
8/29/2019 CSE 1321

5 Java- Define a 2D Array <type> [ ][ ] <name> = new <type>[<rowSize>][<columnSize>]; For example: int[ ][ ] grid = new int [10][20]; 8/29/2019 CSE 1321

6 C# - Define a 2D Array <type>[ , ]<name> = new <type>[<row_size>,<column_size>]; For example: int[ , ] grid = new int [10,20]; 8/29/2019 CSE 1321

7 Modifying a 2D Array myArray[2,3] = 42; 1 2 3 4 5 6 myArray 1 2 3 4 5
1 2 3 4 5 6 [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] myArray 1 [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] 2 [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] 3 [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] 4 [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] 5 [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] 6 [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] myArray[2,3] = 42; 8/29/2019 CSE 1321

8 Time for a secret… You use nested loops any time you have to visit every element Find the min Find the max Find the average Print every element etc. It sounds like a test question… Let’s make a multiplication table

9 CREATE ARRAY grid [4][4]

10 CREATE ARRAY grid [4][4] 4 4

11 CREATE ARRAY grid [4][4] How do we initialize this?

12 FOR each element in a row
CREATE ARRAY grid [4][4] FOR each element in a row END FOR row

13 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row

14 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row row

15 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

16 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

17 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 row

18 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 row

19 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 row

20 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 row

21 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 row

22 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 1 row

23 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

24 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 row

25 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 1 row

26 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 1 row 1

27 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 row 1

28 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 row 1 2

29 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 row 1 2

30 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 row 1 2 3

31 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 1 row 1 2 3

32 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 2 1 2 3 row

33 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 2 3 row

34 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 1 2 3 row

35 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 2 1 2 3 row

36 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 2 1 2 3 row 2

37 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 2 1 2 3 row 2

38 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 2 1 2 3 row 2 4

39 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 2 1 2 3 row 2 4

40 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 2 1 2 3 row 2 4 6

41 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 2 1 2 3 row 2 4 6

42 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 3 1 2 3 2 4 6 row

43 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 2 3 2 4 6 row

44 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 1 2 3 2 4 6 row

45 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 3 1 2 3 2 4 6 row

46 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 1 3 1 2 3 2 4 6 row 3

47 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 3 1 2 3 2 4 6 row 3

48 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 2 3 1 2 3 2 4 6 row 3 6

49 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 3 1 2 3 2 4 6 row 3 6

50 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col col row 3 3 1 2 3 2 4 6 row 3 6 9

51 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR col row 4 3 1 2 3 2 4 6 row 3 6 9

52 FOR each element in a row FOR each element in a col
CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR row 4 1 2 3 2 4 6 3 6 9

53 CREATE ARRAY grid [4][4] FOR each element in a row FOR each element in a col grid[row, col] = row * col END FOR // Continue processing 1 2 3 2 4 6 3 6 9

54 Back to your regular lecture…
8/29/2019 CSE 1321 Module 4

55 Working with 2D arrays Usually involves nested loops, as shown below Problem Statement: Create an array of 4 rows and 5 columns. Populate the array with the numbers 1-20. Create array grid[4][5] count ← 1 FOR each element in a row FOR each element in a column     grid[row][col] = count count ← count + 1 END INNER FOR END FOR 8/29/2019 CSE 1321

56 int count = 1; //declare a 2D array with 4 rows and 5 columns int[][] grid = new int[4][5]; //getting the rows in the array for (int row = 0; row < 4; row++) { //populate the values in the array (row) we're currently on from the outer loop for (int column = 0; column < 5; column++){ grid[row][column] = count; count++; } //for each array in the grid (represented by rows) for(int item[] : grid) //for each element in the array (row) we're currently on from the outer loop for (int number : item) { System.out.print(number + ","); if(number % 5 == 0) { System.out.println(" "); } 8/29/2019 CSE 1321

57 int count = 1; //declare a 2D array with 4 rows and 5 columns int[,] grid = new int[4, 5]; //getting the rows in the array for (int row = 0; row < 4; row++) { //populate the values in the array (row) we're currently on from the outer loop for (int column = 0; column < 5; column++) grid[row, column] = count; count++; } //for each array in the grid (represented by rows) foreach(int number in grid) //for each element in the array (row) we're currently on Console.Write(number + ","); if(number % 5 == 0) { Console.WriteLine(" "); } 8/29/2019 CSE 1321

58 multi-dimension arrays
CREATE array nums [size_1][size_2][size_3] Use nested loops, one for each dimension to traverse the array FOR each number in the row FOR each number in the column FOR each number in the third dimension do some processing ENDFOR 4/26/2018

59 C# multi-dimension arrays
int[,,] nums= new int [5,6,7]; Example 1: 3 nested loops using GetLength(X)   for(int i=0; i<nums.GetLength(0);i++)   {     for (int j=0; j< nums.GetLength(1);j++)     {      for(int k=0; k<nums.GetLength(2);k++)        {              // do some processing       }     }   } 4/26/2018

60 C# multi-dimension arrays
Example 2: Use a foreach loop as long as you aren’t changing the contents of the array foreach ( int i in nums)  {. . .}  public static int findLargest(int[,,]n)        {            int max = n[0,0,0];             foreach (int j in n)                {                     if( j > max)                        max = j;                }             return max;        } 4/26/2018

61 Java multi-dimension arrays
int[][][] nums= new int [5][6][7]; // 3 nested loops using nums.length, nums[0].length and nums[0][0].length for(int i=0; i<nums.length;i++) {   for (int j=0; j< nums[0].length;j++)   {     for(int k=0; k<nums[0][0].length;k++)     {       // do some processing     }   } } 4/26/2018

62 Java multi-dimension arrays
OR - Use the enhanced for loop/for each loop on each subarray structure, as long as you aren’t changing the contents of the array public static int findLargest3(int[][][]n) {   int max = n[0][0][0];             for ( int [][] e : n ){                 for ( int [] d : e ){                  for ( int i : d ){                         if( i > max)           max = i;                    }     }   }             return max;        } 4/26/2018

63 Summary 2D Arrays Multi-dimensional Arrays
Are like 1D arrays in nature Use nested loops Multi-dimensional Arrays Keep nesting loops for each dimension 8/29/2019 CSE 1321


Download ppt "Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7."

Similar presentations


Ads by Google