Two-Dimensional Arrays

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
CS305j Introduction to Computing Two Dimensional Arrays 1 Topic 22 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
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;
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.
Arrays.
int [] scores = new int [10];
Arrays So far we have been dealing with single data items What if you want to handle multiple related data items of the same type? An array is a container.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 9 Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Arrays Chapter 7.
Arrays.
Chapter 7 User-Defined Methods.
Java for Beginners Level 6 University Greenwich Computing At School
EGR 2261 Unit 10 Two-dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Two-Dimensional Arrays
Topic Dimensional Arrays
A simple way to organize data
Arrays in C.
Case Study 2 – Marking a Multiple-choice Test
עקרנות תכנות מונחה עצמים
Control Statement Examples
Arrays Declare the Array of 100 elements Notes
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
Yong Choi School of Business CSU, Bakersfield
One-Dimensional Array Introduction Lesson xx
Board: Objects, Arrays and Pedagogy
Nested Loop Review and Two-Dimensional Arrays
Topic 26 Two Dimensional Arrays
Two-Dimensional Arrays
EKT150 : Computer Programming
int [] scores = new int [10];
Review of Arrays and Pointers
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
More 2 D Array.
2D Array and Matrix.
Data Structures (CS212D) Week # 2: Arrays.
Two-Dimensional Arrays
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
Arrays and Array Lists CS 21a.
Multidimensional Arrays
Arrays Week 2.
Arrays in Java.
Continue with Life, Magic and Catch -up
Review of Previous Lesson
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Chapter 8 Multidimensional Arrays
AP Java Learning Objectives
C++ Array 1.
How do you do the following?
Presentation transcript:

Two-Dimensional Arrays Start slide 9 on Tuesday

Learning Objectives Introduce two-dimensional arrays Be able to declare a two-dimensional array Be able to analyze the values in each row of a 2D array Understand the implications of an array of arrays. Describe the use of two-dimensional arrays to represent grids of information

Two-Dimensional Arrays Declaration similar to one dimensional arrays Need to specify both the number of rows and columns during allocation Example: final int COLS = 6, ROWS = 5; //Constants double[][]energyTable = new double[ROWS][COLS];

Arrays of Arrays When we write This is shorthand for double[][] energyTable = new double[ROWS][COLS]; This is shorthand for double[][] energyTable = new double[ROWS][]; for (int i = 0; i < ROWS; i++) energyTable[i] = new double[COLS];

What do you think this would do? int [][] test = new int [3][]; test[0] = new int[5]; test[1] = new int [2]; test[2] = new int[3]; Draw a picture of the array being defined.

2-D Arrays: Dimensions In Java, a 2-D array is a 1-D array of 1-D arrays, its rows. Each row is stored in a separate block of consecutive memory locations. If m is a 2-D array, then m[k] is a 1-D array, the k-th row. m.length is the number of rows. m[k].length is the length of the k-th row. Nothing unusual: an array of rows contains objects; each object happens to be an array.

More Two D Internally, Java stores 2 dimensional arrays as an array of arrays: int [][] nums = new int[5][4]; The above is really equivalent to a 3-step process: // create the single reference nums (yellow square) int [][] nums; // create the array of references (blue squares) nums = new int[5][]; // this create the second level of arrays (red squares) for (int i=0; i < 5 ; i++) nums[i] = new int[4]; // create arrays of integers

Even More 2-D Note: when you initially declare a 2D array: you must always specify the first dimension nums = new int[][]; // ILLEGAL - NEEDS 1ST DIMENSION you do not need to specify the second dimension nums = new int[5][]; // OK nums = new int[5][4]; // OK Elements of the Array: if nums is a 2D array as shown above, nums[i][j] represents a single integer in that array nums[i] represents a 1D array (a single row in the 2D array)

Review: Declaration int board[][] = new int[3][3]; Object sample[][] = new Object[4][4]; double trouble[][] = {{1.2, 2.3},{2.1,3.1}, {6.2, 10.0}} Dry a picture of the following array String wurds[][] = new String[5][]; for (int row=0; row<wurds.length; row++) { wurds[row]= new String[row+2]; } What is the value of wurds[2].length ? What type of value is wurds[1]? What type of value is wurds[2][1]?

public static void main(String [] args) { final char BLANK = ' public static void main(String [] args) { final char BLANK = '?'; // location empty final char X = 'x'; final char OH = 'o'; final int size = 3; char board[][] = new char[size][size]; for (int i=0; i < size; i++) for (int j=0; j < size; j++) board[i][j] = BLANK; for(int i=0; i< size; i++) board[i][i] = X; for (int j = 0; j< size; j++) board[j][size-j-1] = OH; System.out.print(board[i][j]+" "); System.out.println(); } Dry Run

Back to this example? int [][] test = new int [3][]; test[0] = new int[5]; test[1] = new int [2]; test[2] = new int[3]; How can you fill this array with random integers from 1 to 50? How can you show all of the values in the array test? How could you calculate the total for all of the values in test? How can you calculate the maximum value in the array? The maximum value from each row? Create a Java program in BlueJ to test the ideas.

Computing Max Values in Each Row int [] max = new int[test.length]; for (int row = 0; row < test.length; row++) { // Set max for each row to 0 max[row] = 0; for (int column =0; column < test[row].length; column++) if (test[row][column] > max[row]) max[row] = test[row][column]; } for(int row = 0 ; row<test.length ; row++) System.out.println( row + " max value = " + max[row]);

Adding Another Dimension You can create arrays of higher dimension than 2. For example, if we were measuring the temperature in a rectangular volume. int temperature[][][] = new int[10][20][30]; This creates an array of 10x20x30=6000 integers. temperature is an array of array of arrays SubArrays: temperature is a 3D array of size 10x20x30. There is one of these. temperature[i] is a 2D array of size 20x30. There are 10 of these. temperature[i][j] is a 1D array of size 30. There are 200 of these. All but the last dimension must be initially specified: int temperature[][][] = new int[10][10][]; // OK int temperature[][][] = new int[10][][]; // NOT OK

Program: Life The universe is a two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated". Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: Any live cell with fewer than two live neighbors dies, as if caused by under population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Patterns - Any live cell with fewer than two live neighbors dies, as if caused by under population. - Any live cell with two or three live neighbors lives on to the next generation. - Any live cell with more than three live neighbors dies, as if by overpopulation. - Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Your application of Life You determine the size of the universe Fill your universe: Examples Each cell has a 1 in N chance of being alive for the first generation. You select the value for N or have the user enter the value for N. Look up potential life patterns. Run the game for several generations. Show the universe after each generation Push: Break the program into Methods. neighbors(), live(), die(), showUniverse(), populate() Push: Determine a way to use Objects in your solution. Push: Have your universe ‘wrap’