Chapter 2: Arrays - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Chapter 2: Arrays

Description:

An array might be comprised of elements that are all strings; another might be ... the subscript value that is to be allowed to take on its entire rage of value. ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 20
Provided by: Dew
Category:
Tags: arrays | chapter | rage

less

Transcript and Presenter's Notes

Title: Chapter 2: Arrays


1
Chapter 2 Arrays
2
Arrays
  • An array is a finite, ordered set of same data
    type element.
  • Also called tables.
  • An array might be comprised of elements that are
    all strings another might be comprised of
    elements that are all integers.

3
One-Dimensional Arrays
  • Also called vector
  • A notation A(LU) indicates that array named A
    with element of same data type having subscripts
    or index which identify the position of each
    element from L through U.
  • For example, Score(120), Name(1040)
  • The number of elements in an array is called its
    range. The range of array A(LU) is U-L1
  • For example, The range of array Score(120) is
    20.
  • The range of array Name(1040) is 40-101 31.

position
String name
4
Multi-Dimensional Arrays
  • Two-Dimensional Array
  • is an array in which each element is itself an
    array.
  • For example, B(1M,1N) is an array named B which
    has row from 1 to M and column from 1 to N.
  • To identify an individual element, two subscript
    values are needed.
  • The first subscript refers to row of array,
    second subscript refers to a column of the array.
  • Total number of elements in array B is MN

5
Multi-Dimensional Arrays
  • A two-dimensional array B with its first
    subscript having lower bound L1 and upper bound
    U1 and is second subscript having lower bound L2
    and upper bound U2 is defined as B(L1U1,L2U2)
  • The number of element in a row of B is U2 - L2
    1
  • The number of element in a column of B is U1 -
    L1 1
  • The total number of elements in array B is
  • (U2 - L2 1)(U1 - L1 1)

6
Multi-Dimensional Arrays
  • Cross-Section
  • A Cross-Section of a two dimensional array is
    obtained by holding one of the subscripts
    constant while varying the other through its
    entire range of values.
  • The asterisk () is used for the subscript value
    that is to be allowed to take on its entire rage
    of value.
  • For example, B(,4) GRADE(2,)

GRADE(120,14)
7
Multi-Dimensional Arrays
  • Transpose
  • The transpose of a two-dimensional array is
    obtained by reversing the subscript position.
  • The transpose of M-by-N array is an N-by-M array.
  • The transpose of array B is commonly demoted BT
  • For example, the transpose of array B(3,8) is
    denoted as BT(8,3)

8
Multi-dimensional Array
  • An array can be defined to be three-dimensional,
    four-dimensional, or N-dimensional arrays.

J
N
M
9
Multi-dimensional Array
  • An N-dimensional array A could be identified as
  • A( L1U1 ,L2U2 ,L3U3 , LNUN )
  • And the total number of elements in array A
    (N-dimensional array) is
  • (U1 L1 1)(U2 L2 1)(UN LN 1)
  • the cross-section normally is formed by holding
    one or more index values constant and varying
    each of the other indexes through its entire
    range of values. i.e. A(5, , 3)

10
Mapping to StorageOne-Dimensional Arrays
  • To calculate the starting address (i.e., the
    location) of element A(I)
  • B is arrays starting address of base location
  • I is the ith element of array
  • L is the lower bound of the array which can be
    positive, negative, or zero
  • S is size of each element in the array

B (I L) S
11
Mapping to Storage Multi-Dimensional Arrays
  • Row-major Order
  • Because computer memory is linear, a
    muti-dimensional array must be linearized as it
    is mapped to storage.
  • Row-major order stores the first row of the
    array, then the second row, then the third row,
    and so forth.

Row 0
Row 1
Row 2
Row -1
Row -2
A(0,6)
A(0,6)
12
Mapping to Storage Multi-Dimensional Arrays
  • The element A(I,J) of array defined by
    A(L1U1,L2U2) is at location
  • For example, the starting location of A(0,6) in
    an array A(-22,46) is at
  • B (0 (-2)) (6 4 1) S (6 - 4) S
  • There are two rows (row -2 and row-1), each of
    length 3S, preceding row 0.
  • In row 0, there are 2 (i.e., 6-4) elements, each
    of length S, preceding element A(0,6)

B (I L1) (U2 L2 1) S (J L2) S
13
Mapping to Storage Multi-Dimensional Arrays
  • Column-major Order
  • Column-major order stores first the first column
    of the array, then the second column of the
    array, then the third column and so forth.

1
2
3
4
5
Col 3
Col 4
Col 5
Col 2
Col 1
Rate(2,4)
1
2
3
Rate(2,4)
  • The element ARRAY(I,J) of the array defined by
    ARRAY(L1U1,L2U2), with column-major storage, is
    at location

B (J L2) (U1 L1 1) S (I L1) S
14
Mapping to Storage Multi-Dimensional Arrays
  • Column-major Order
  • For example, the array rate(13,15) is stored in
    memory by column. If base location B 0 and
    element size S 1, the starting address of
    element rate(2,4) is
  • 0 (4 1) (3 1 1) 1 (2 1) 1 10

15
Triangular Arrays
  • Two types of triangular array
  • Upper triangular all elements below the
    diagonal are zero.
  • Lower triangular all elements above the
    diagonal are zero.
  • An array is called strictly upper-(lower)
    triangular if the elements of the diagonal are
    also zero.

Lower--triangular array
Upper-triangular array
16
Triangular Arrays
  • In a lower-triangular array with N rows, the
    maximum number of nonzero elements in the ith row
    is I.
  • The total number of nonzero elements (both
    upper-triangular array and lower triangular
    array) is no more than

17
Triangular Arrays
  • Linearization
  • Lenearize the array and store only the elements
    of the nonzero triangular portion.
  • For example,

18
Sparse Arrays
  • An array that has high density of zero elements

8 by 10 sparse array
19
Sparse Arrays
  • Vector Representation
  • Each nonzero element in a two-dimensional sparse
    array can be represented as a triple with the
    format (row-subscript, column-subscript, value).
Write a Comment
User Comments (0)
About PowerShow.com