Arrays.

Slides:



Advertisements
Similar presentations
Matrices and MATLAB Dr Viktor Fedun
Advertisements

Chapter 6 Data Structures
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.
Gauss – Jordan Elimination Method: Example 2 Solve the following system of linear equations using the Gauss - Jordan elimination method Slide 1.
Matrices CSE, POSTECH.
Arrays.
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Section 1.7 Diagonal, Triangular, and Symmetric Matrices.
1.7 Diagonal, Triangular, and Symmetric Matrices.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
ISBN Chapter 6 Data Types: Structured types.
Psychology 202b Advanced Psychological Statistics, II January 18, 2011.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Matrix table of values
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Chapter 1: Matrices Definition 1: A matrix is a rectangular array of numbers arranged in horizontal rows and vertical columns. EXAMPLE:
1.7 Diagonal, Triangular, and Symmetric Matrices 1.
Table of Contents Solving Systems of Linear Equations - Gaussian Elimination The method of solving a linear system of equations by Gaussian Elimination.
Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH. 2 2 Any real-life matrix you know?
SNU IDB Lab. Ch8. Arrays and Matrices © copyright 2006 SNU IDB Lab.
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.
Equivalence Relations
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
Assembly - Arrays תרגול 7 מערכים.
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
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.
Introduction Types of Matrices Operations
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
MATRICES A rectangular arrangement of elements is called matrix. Types of matrices: Null matrix: A matrix whose all elements are zero is called a null.
MTH108 Business Math I Lecture 20.
Linear Algebraic Equations and Matrices
nhaa/imk/sem /eqt101/rk12/32
Two-Dimensional Arrays
1.5 Matricies.
Java Array Object Chuen-Liang Chen Department of Computer Science
Expressions and Assignment
Linear Algebraic Equations and Matrices
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Reminder: Array Representation In C++
Multiple Dimension Arrays
بردارها و ماتريس ها ساختمان داده ها
Reminder: Array Representation In C++
Introduction to Python
Matrix Algebra - Overview
Declaration, assignment & accessing
Review of Arrays and Pointers
Graph Operations And Representation
Arrays .
MSIS 655 Advanced Business Applications Programming
RECORD. RECORD COLLABORATE: Discuss: Is the statement below correct? Try a 2x2 example.
Determinant of a Matrix
Arrays.
Arrays Week 2.
Dr Tripty Singh Arrays.
Multi-Dimensional Arrays
Arrays.
Arrays.
Laboratory in Oceanography: Data and Methods
C++ Array 1.
Reminder: Array Representation In C++
Arrays and Matrices Prof. Abdul Hameed.
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

Arrays

1D Array Representation In C++ Memory a b c d start 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Location: Location in memory location(x[i]) = start + i (assuming unit size for elements)

Space Overhead space overhead = 4 bytes (32 bits) for start Memory a b c d start space overhead = 4 bytes (32 bits) for start (excludes space needed for the elements of x) (excludes space needed for other variables such as listSize and array length) What about #bytes/element of array? Excluded in above analysis.

2D Arrays The elements of a 2-dimensional array a declared as: int a[3][4]; (We refer to such an array as an M-by-N array) may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] M=3, N=4

Rows Of A 2D Array a[0][0] a[0][1] a[0][2] a[0][3] row 0

Columns Of A 2D Array a[0][0] a[0][1] a[0][2] a[0][3] The mathematical abstraction corresponding to such tables is a Matrix.

2D Array Representation In C++ 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 4 1D arrays

2D Array Representation In C++ b c d e f g h i j k l x[] The 4 separate arrays are x, x[0], x[1], and x[2]. 4 separate 1-dimensional arrays

Space Overhead space overhead = overhead for 4 1D arrays = 4 * 4 bytes g h i j k l x[] space overhead = overhead for 4 1D arrays = 4 * 4 bytes = 16 bytes = (number of rows + 1) x 4 bytes (One ‘start’ pointer for x[]  4 bytes) + (Numbers of rows  numbers of ‘start’ pointers  4 bytes each)

Array Representation In C++ b c d e f g h i j k l x[] This representation is called the array-of-arrays representation. Requires contiguous memory of size 3, 4, 4, and 4 for the 4 1D arrays. 1 memory block of size number of rows and number of rows blocks of size number of columns

Array of arrays The same notion extends to multi-dimensional arrays (e.g. 3D Array) Enables jagged arrays: int jagged_row0[] = {0,1}; int jagged_row1[] = {1,2,3}; int* jagged[] = { jagged_row0,jagged_row1 };

Row-Major Mapping Example 3 x 4 array: a b c d e f g h i j k l Convert into 1D array y by collecting elements by rows. Within a row elements are collected from left to right. Rows are collected from top to bottom. We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l} Used in Pascal and Fortran for example. row 0 row 1 row 2 … row i

Locating Element x[i][j] row 0 row 1 row 2 … row i c 2c 3c ic assume x has r rows and c columns each row has c elements i rows to the left of row i so ic elements to the left of x[i][0] so x[i][j] is mapped to position ic + j of the 1D array

Space Overhead 4 bytes for start of 1D array + row 0 row 1 row 2 … row i 4 bytes for start of 1D array + 4 bytes for c (number of columns) = 8 bytes (regardless of number of rows) Although, for our example the difference in space overhead between the two representation methods is small, the space overhead for the row-major scheme is 8 bytes regardless of the number of rows in the array. In the array of arrays scheme, the space overhead is 4 * (r+1) bytes. So the overhead for an array with 10000 rows Is 40004 bytes when the array of array scheme is used and only 8 bytes when the row-major scheme is used.

Need contiguous memory of size rc. Disadvantage Need contiguous memory of size rc.

Column-Major Mapping a b c d e f g h i j k l Convert into 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. We get y = {a, e, i, b, f, j, c, g, k, d, h, l}

Representations in some programming languages Array of arrays: Java, Scala, JavaScript, PHP Row-major: Pascal, Mathematica, Numpy (for Python) Column-major: Fortran, MATLAB, R, Julia

Matrix Table of values. Has rows and columns, but numbering begins at 1 rather than 0. a b c d row 1 e f g h row 2 i j k l row 3 Use notation x(i,j) rather than x[i][j]. May use a 2D array to represent a matrix.

Shortcomings Of Using A 2D Array For A Matrix Indexes are off by 1. C++ arrays do not support matrix operations such as add, transpose, multiply, and so on. Suppose that x and y are 2D arrays. Can’t do x + y, x –y, x * y, etc. in C++. Develop a class matrix for object-oriented support of all matrix operations. See text. Matrix operations are supported in 3rd party libraries e.g. Armadillo The class Matrix uses the row-major representation of a matrix.

An n x n matrix in which all nonzero terms are on the diagonal. Diagonal Matrix An n x n matrix in which all nonzero terms are on the diagonal. The text considers several kinds of special matrices—diagonal, tridiagonal, lower triangular, upper triangular, and symmetric. Applications are given in the text and exercises.

Diagonal Matrix 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 x(i,j) is on diagonal iff i = j number of diagonal elements in an n x n matrix is n non diagonal elements are zero store diagonal only vs n2 whole

Lower Triangular Matrix An n x n matrix in which all nonzero terms are either on or below the diagonal. 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 x(i,j) is part of lower triangle iff i >= j. number of elements in lower triangle is 1 + 2 + … + n = n(n+1)/2. store only the lower triangle

Array Of Arrays Representation 1 2 3 4 5 6 x[] 7 8 9 l0 Use an irregular 2-D array … length of rows is not required to be the same.

Creating And Using An Irregular Array // declare a two-dimensional array variable // and allocate the desired number of rows int ** irregularArray = new int*[numberOfRows]; // now allocate space for elements in each row for (int i = 0; i < numberOfRows; i++) irregularArray[i] = new int [length[i]]; // use the array like any regular array irregularArray[2][3] = 5; irregularArray[4][6] = irregularArray[2][3]+2; irregularArray[1][1] += 3;

Map Lower Triangular Array Into A 1D Array Use row-major order, but omit terms that are not part of the lower triangle. For the matrix 1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 10 we get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 If the 1D array that is used to store the lower triangle is y, then following the above mapping y[i] = i +1 . y[] = {1, 2, 3, 4, …, 10}

Index Of Element [i][j] r 1 r2 r3 … row i 1 3 6 Order is: row 1, row 2, row 3, … Row i is preceded by rows 1, 2, …, i-1 Size of row i is i. Number of elements that precede row i is 1 + 2 + 3 + … + i-1 = i(i-1)/2 So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array. When using the irregular array mapping of a lower triangular matrix we are able to use conventional Java syntax to access elements, but for large n, the space overhead is quite a bit more than when the row-major mapping into a 1D array is used. The row-major mapping requires you to develop a formula to access elements.

To-do http://www.cplusplus.com/doc/tutorial/arrays/ https://en.wikipedia.org/wiki/Matrix_(mathematics)