Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Building Java Programs Chapter 7.5
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
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.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Java Programming: From Problem Analysis to Program Design, 4e
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ARRAYS Multidimensional realities Image courtesy of
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Two-Dimensional Arrays
C++ Array 1.
Arrays.
Presentation transcript:

Two Dimensional Arrays

Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D arrays methods in programs.

Vocabulary COLUMN MATRIX ROW LENGTH

Intro to 2D Arrays Two-dimensional arrays allow the programmer to solve problems involving rows and columns. Many data processing problems involve rows and columns, such as an airplane reservation system or the mathematical modeling of bacteria growth.

Intro to 2D Arrays A classic problem involving two- dimensional arrays is the bacteria simulation program presented in the lab exercise, Life. After surveying the syntax and unique aspects of these larger data structures, such information will be applied to more challenging lab exercises.

Two-Dimensional Arrays Often the data a program uses comes from a two dimensional situation. For example, maps are two-dimensional (or more), the layout of a printed page is two-dimensional, a computer-generated image (such as on your computer's screen) is two dimensional, and so on.

Two-Dimensional Arrays For these situations, a Java programmer can use a two-dimensional array. This allows for the creation of table-like data structures with a row and column format. The first subscript will define a row of a table with the second subscript defining a column of a table. Here is an example program including a diagram of the array.

Code to put the values in table: table

Two Dimensional Syntax int[][] table = new int[3][4]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) table[row][col] = row + col;

Two Dim Syntax int[][] table = new int[3][4]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) table[row][col] = row + col; Double brackets

Two Dim Syntax int[][] table = new int[3][4]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) table[row][col] = row + col; Double brackets # of rows

Two Dim Syntax int[][] table = new int[3][4]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) table[row][col] = row + col; Double brackets # of rows # of cols

Two Dim Syntax int[][] table = new int[3][4]; int row, col; for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) table[row][col] = row + col; Double brackets # of rows # of cols Think RC-Cola!

Two-Dimensional Arrays Two-dimensional arrays are objects. A variable such as table is a reference to a 2D array object. The declaration int[][] table; says that table can hold a reference to a 2D array of integers. Without any further initialization, it will start out holding null.

Two-Dimensional Arrays The declaration int[][]table = new int[3][4]; says that table can hold a reference to a 2D array of integers, creates an array object of 3 rows and 4 columns, and puts the reference in table. All the elements of the array are initialized to zero.

Two-Dimensional Arrays The declaration int[][]table = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; does exactly the same thing as the previous declaration (and would not ordinarily be used.)

Two-Dimensional Arrays The declaration: int[][]table = { {0,1,2,3}, {1,2,3,4}, {2,3,4,5} }; creates an array of the same dimensions (same number of rows and columns) as the previous array and initializes the elements to the same values in the first example.

Two-Dimensional Arrays If no initializer is provided for an array, then when the array is created it is automatically filled with the appropriate value: zero for numbers, false for boolean, and null for objects. Just as with one-dimensional arrays, the row and column numbering of a 2-D array begin at subscript location zero (0). The 3 rows of the table are referenced from rows Likewise, the 4 columns of the table are referenced from columns

Two-Dimensional Arrays To access each location of the matrix, specify the row coordinate first, then the column: table[row][col] Each subscript must have its own square brackets.

Two-Dimensional Arrays The length of a 2D array is the number of rows it has. The row index will run from 0 to length- 1. The number of rows in table are given by the value table.length. Each row of a 2D array has its own length. To get the number of columns in table, use any of the following: table[1].length table[2].length table[3].length

Two-Dimensional Arrays There is actually no rule that says that all the rows of an array must have the same length, and some advanced applications of arrays use varying-sized rows. But if you use the new operator to create an array in the manner described above, you'll always get an array with equal-sized rows.

Two-Dimensional Arrays The routine that assigned values to the array used the specific numbers of rows and columns. That is fine for this particular program, but a better definition would work for an array of any two dimensions.

Two-Dimensional Arrays for (row = 0; row < table.length; row++) for (col = 0; col < table[row].length; col++) table[row][col] = row + col;

Two-Dimensional Arrays for (row = 0; row < table.length; row++) for (col = 0; col < table[row].length; col++) table[row][col] = row + col; What does this mean?

Two-Dimensional Arrays as Parameters Look at Test2D.java The loadTable and printTable methods each use a reference parameter, ( int[][] lTable and int[][] pTable respectively). The local identifiers lTable and pTable serve as aliases for the actual parameter grid passed to the methods.

Two-Dimensional Arrays as Parameters When a program is running and it tries to access an element of an array, the Java virtual machine checks that the array element actually exists. This is called bounds checking. If your program tries to access an array element that does not exist, the Java virtual machine will generate an ArrayIndexOutOfBoundsException exception. Ordinarily, this will halt your program.

Two-Dimensional Array Algorithms The most common 2-D array algorithms will involve processing the entire grid, usually row-by-row or column-by-column. Problem-solving on a matrix could involve processing: a. one rowb. one column c. one celld. adjacent cells in various different directions.

In conclusion…… Two-dimensional arrays will be applied to two interesting problems. The simulation of life in a petri dish of bacteria will require a two-dimensional array representation. The second and third lab exercises are different versions of the "Knight's Tour" problem, an interesting and demanding chess movement problem.

Next Lab Irregular Polygon You may use the built in Point2D class.

The most common 2-D array algorithms will involve processing the entire grid, usually row-by- row or column-by-column. More advanced pproblem-solving on a matrix could involve processing: a. one row b. one column c. one cell d. adjacent cells in various different directions. Be sure you can write code for any of these. Challenge Problem This is the second little challenge for you. Just a quick problem to keep you thinking. The first three (3) students to page me the correct solution will receive bonus points. Given the following code as part of a program. String[][] myTable = { {"Bruins", "Rangers", "Canadians" }, {"Maple Leafs","Blackhawks","Red Wings"} }; Write the code to print a "2 X 3 table" with the title: "NHL - Original Six" centered above the teams. The team names are centered in fields of width 16. Output should be similar to the picture below.