Lecture 4 2d Arrays CSE 1322 4/26/2018.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Building Java Programs Chapter 7.5
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
Multiple Choice Solutions True/False a c b e d   T F.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
CS 201 Tarik Booker California State University, Los Angeles.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Looping Examples I.
Lecture 11 Multi-dimensional Arrays
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Two-Dimensional Arrays
Topic Dimensional Arrays
Computer Programming BCT 1113
multi-dimensional arrays
Two Dimensional Arrays
Two-dimensional arrays
Two Dimensional Array Mr. Jacobs.
Two-Dimension Arrays Computer Programming 2.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
ECE Application Programming
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
ECE Application Programming
Nested Loop Review and Two-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
1020: Introduction to Programming Mohamed Shehata November 22, 2017
EKT150 : Computer Programming
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Lecture 6 2d Arrays Richard Gesick.
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Multidimensional Arrays
2D Arrays Just another data structure Typically use a nested loop
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Multi-Dimensional Arrays
Review of Classes and Arrays
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Lecture 14 2D Arrays Richard Gesick.
Multi-Dimensional Arrays
EECE.2160 ECE Application Programming
Dr. Sampath Jayarathna Cal Poly Pomona
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
Multidimensional Arrays Section 6.4
Arrays in MatLab Arrays can have any number dimensions
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Review of Classes and Arrays
Arrays Introduction to Arrays Reading for this Lecture:
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
MIS 222 – Lecture 12 10/9/2003.
Visit for more Learning Resources
Presentation transcript:

Lecture 4 2d Arrays CSE 1322 4/26/2018

2d arrays Working with 2D arrays is similar to 1D arrays.  But realize you need to specify two dimensional sizes and any time you access an element, you need to specify both indices. 4/26/2018

CREATE array nums [size_1][size_2] define a 2D array CREATE array nums [size_1][size_2] 4/26/2018

C# define a 2D array <type>[ , ]<name> = new<type>[<size_1>,<size_2>]; For example: int[ , ] grid = new int [10,20]; 4/26/2018

Java define a 2D array <type>[ , ]<name> = new<type>[<size_1>][<size_2>]; For example: int[ ][ ] grid = new int [10][20]; 4/26/2018

Working with 2D arrays usually involves nested loops like this using a array called grid: FOR each element in a row FOR each element in a column     grid[row][col] = col * row; 4/26/2018

Working with 2D arrays usually involves nested loops like this in java: for(int row=0; row < 10; row++)   for(int col=0; col < 20; col++)     grid[row][col] = col * row; 4/26/2018

Working with 2D arrays usually involves nested loops like this in C#: for(int row=0; row < 10; row++)   for(int col=0; col < 20; col++)     grid[row,col] = col * row; 4/26/2018

What is generated? Java: C# int[,] grid = new int[10, 20]; for (int r = 0; r< 10;r++) for (int c = 0; c < 20; c++) grid[r, c] = c* r; int[][] grid = new int[10][20]; for (int r = 0; r< 10;r++) for (int c = 0; c < 20; c++) grid[r][c] = c* r; 4/26/2018

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

C# multi-dimension arrays int[,,] nums= new int [5,6,7]; 3 nested loops using GetLength(0), GetLength(1) and GetLength(2) 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

C# multi-dimension arrays int[,,] nums= new int [5,6,7]; Or 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

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

Java multi-dimension arrays int[][][] nums= new int [5][6][7]; 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