1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
Matrix Multiplication To Multiply matrix A by matrix B: Multiply corresponding entries and then add the resulting products (1)(-1)+ (2)(3) Multiply each.
Maths for Computer Graphics
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Arrays Chapter 6 Chapter 6.
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.
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
“No one can be told what the matrix is…
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Add, Subtract, and Multiply Matrices. A matrix M is an array of cell entries (m row,column ) that have rectangular dimensions (Rows x Columns). Example:
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4.1 Using Matrices to Represent Data
Matrix Arithmetic. A matrix M is an array of cell entries (m row,column ) and it must have rectangular dimensions (Rows x Columns). Example: 3x x.
13.1 Matrices and Their Sums
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
AIM: How do we perform basic matrix operations? DO NOW:  Describe the steps for solving a system of Inequalities  How do you know which region is shaded?
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Sec 4.1 Matrices.
Copyright © Cengage Learning. All rights reserved. 2 SYSTEMS OF LINEAR EQUATIONS AND MATRICES.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
Matrices Digital Lesson. Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 A matrix is a rectangular array of real numbers. Each entry.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Arrays.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
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.
14.1 Matrix Addition and Scalar Multiplication OBJ:  To find the sum, difference, or scalar multiples of matrices.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Designed by Victor Help you improve MATRICES Let Maths take you Further… Know how to write a Matrix, Know what is Order of Matrices,
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Properties and Applications of Matrices
12-1 Organizing Data Using Matrices
Multidimensional Arrays
Matrix Operations.
Matrix Multiplication
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Dr. Ameria Eldosoky Discrete mathematics
Multiplying Matrices.
WarmUp 2-3 on your calculator or on paper..
Chapter 7 Multidimensional Arrays
25. Basic matrix operations
MATRICES MATRIX OPERATIONS.
Multidimensional array
Multiplying Matrices.
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Multi-Dimensional Arrays
Chapter 8 Multidimensional Arrays
1.8 Matrices.
1.8 Matrices.
Multiplying Matrices.
Arrays and Matrices Prof. Abdul Hameed.
Multiplying Matrices.
Multiplying Matrices.
Presentation transcript:

1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example: drivers & trips Example: working with matrices

2 Arrays Previously we saw we can have an array of elements Elements of such arrays are accessed using a subscript. Example int [] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Subscripts begin at 0 month[0] is the first element and has the value 31 month[5] is the sixth element and has the value 30 All elements are of the same type – in this case int All elements can be accessed readily using a for for (int numDays:daysInMonth) System.out.println(numDays);

3 Arrays of Arrays When the elements of an array are themselves arrays then we have multi-dimensional arrays, or, arrays of arrays Example. Consider a table, times, that represents a times table: times

4 Arrays of Arrays The product, 4 x 6, is in the cell found at the intersection of the 4 th row and 6 th column As a 2-dimensional array we can access this value as times[3][5] th row 6 th column

5 Arrays of Arrays We can declare this times table in Java as int[][] times ={{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}, {4, 8, 12, 16, 20, 24}, {5, 10, 15, 20, 25, 30}, {6, 12, 18, 24, 30, 36 } } each element is an array. 1 st subscript is row. 2 nd subscript is column, or equivalently, an index into the row. All values are enclosed in { } Each row is enclosed in { } All elements are comma-separated. times[3][5] references value in the 4 th row and within the row, the 6 th entry.

6 Arrays of Arrays Consider the following code that prompts for 2 integers between 1 and 6 and reports their product: int[][] times ={{1, 2, 3, 4, 5, 6}, {2, 4, 6, 8, 10, 12}, {3, 6, 9, 12, 15, 18}, {4, 8, 12, 16, 20, 24}, {5, 10, 15, 20, 25, 30}, {6, 12, 18, 24, 30, 36 } }; Scanner kb = new Scanner(System.in); System.out.println("enter two integers between 1 and 6" +" and I will tell you the product"); int i = kb.nextInt(); int j = kb.nextInt(); int p = times[i-1][j-1]; System.out.println("The product of "+i+" and "+j+" is "+p);

7 Representation in memory The JVM stores, in the memory location of the variable times, a reference to its elements (an array of …). Each element has a reference to storage locations representing the column values that make up a row. Since these are just int s these values are stored in those locations. times arrays of 6 elements each An array of 6 elements

8 Creating a 2D array As shown previously we can initialize an array of arrays. We can also declare the array and assign it values through calculations or input. Consider the following code where we declare and initialize a times table that is 10 by 10: int[][] times = new int[10][10]; for (int r= 0; r<10; r++) for (int c= 0; c< 10; c++) times[r][c] = (r+1) * (c+1); Assignment statement … the element in the r th row and c th column is set to (r+1)*(c+1)

9 Ragged arrays Java implements multidimensional arrays as arrays of arrays Recall that an array has a length field specifying how many elements the array contains. As arrays can be of different lengths, then for a 2D array each row can be a different size. Each array has its own length field. Question: for a 10 x 10 times table, how many length fields are there?

10 Ragged arrays Consider Figure 2.4 which illustrates the km driven per trip for each driver

11 Ragged arrays As in program DriversTrips we can initialize the array: int[][] trips ={ {25, 29, 30, 40}, {44, 25}, {22, 27, 55, 33, 80}, {55, 57, 45}, {31, 42, 49, 46} }; Row has 4 elements Row has 2 elements

12 Ragged arrays As in program DriversTrips we can display the trips for each driver for (int i=0; i<trips.length; i++){ System.out.print("driver: "+i+"\t"); // number of trips for ith driver // is trips[i].length for (int j=0;j<trips[i].length;j++){ System.out.print(trips[i][j]+"\t"); } System.out.println(); } Driver row i Trip column j Size of i th row ≡ number of trips for i th driver number of drivers

13 Ragged arrays How many kilometers has each driver driven? To calculate this we must calculate row sums. for (int i=0; i<trips.length; i++){ System.out.print("driver: "+i+"\t"); // calculate row sum for driver int sum = 0; for (int j=0;j<trips[i].length;j++){ sum += trips[i][j]; } System.out.print(sum+"\n"); }

14 Matrices A 2D matrix is a mathematical construct of rows and columns. For example Is a 2D matrix of 2 rows and 3 columns. Sometimes called a 2 by 3 matrix [ ]

15 Matrices Mathematical operations (add, subtraction, scalar multiplication, transposition, multiplication, etc.) are defined for matrices. Scalar multiplication. Consider the product c*A where c is a scalar value and A is a matrix, then c*A is computed by multiplying each element of A by the value c. [ ] =

16 Matrices – scalar multiplication Consider the code below that computes C=2*A. int[][] a ={ {1, 2, 3}, {6, 5, 4} }; int s = 2; int [][] c = new int[2][3]; for (int i=0; i< a.length; i++) for (int j=0; j<a[i].length; j++) c[i][j] = s *a [i][j];

17 Matrices - addition Matrix addition. to add two matrices of equal size we just add elements in corresponding positions. [ ] =

18 Matrices - addition The program MatrixAddition on page 37 adds two matrices. Consider the code The code is simple enough but writing such code requires care due to the different subscripts names (i & j), the different lengths (rows & columns), and the syntax requirements of Java // C = A + B // For each c[i][j] in C // c[i][j] = a[i][j]+b[i][j] // A and B must have the same // number of rows and columns for (int i=0; i< a.length; i++) for (int j=0; j<a[i].length; j++) c[i][j] =a [i][j] + b[i][j];

19 Matrices - addition The program MatrixAddition on page 37 does this. Consider the code The code is simple enough but writing such code requires care due to the different subscripts (i & j), the different lengths, and the syntax requirements of Java // C = A + B // For each c[i][j] in C // c[i][j] = a[i][j]+b[i][j] // A and B must have the same // number of rows and columns for (int i=0; i< a.length; i++) for (int j=0; j<a[i].length; j++) c[i][j] =a [i][j] + b[i][j];