Array Abstract Data Type



Similar documents
One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

DATA STRUCTURES USING C

Base Conversion written by Cathy Saxton

VB.NET Programming Fundamentals

Matrix Multiplication

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Arrays in Java. Working with Arrays

Section of DBMS Selection & Evaluation Questionnaire

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

arrays C Programming Language - Arrays

Operation Count; Numerical Linear Algebra

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

8 Square matrices continued: Determinants

Chapter 7: Additional Topics

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Binary Adders: Half Adders and Full Adders

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Section 3-3 Approximating Real Zeros of Polynomials

In this Chapter you ll learn:

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

Solution of Linear Systems

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

AP Computer Science Java Mr. Clausen Program 9A, 9B

Glossary of Object Oriented Terms

5 Arrays and Pointers

Module 2 - Multiplication Table - Part 1-1

I PUC - Computer Science. Practical s Syllabus. Contents

Direct Methods for Solving Linear Systems. Matrix Factorization

Row Echelon Form and Reduced Row Echelon Form

Information technology Programming languages Fortran Enhanced data type facilities

FACTORING QUADRATICS and 8.1.2

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

SmartArrays and Java Frequently Asked Questions

Record Storage and Primary File Organization

Lecture 2. Binary and Hexadecimal Numbers

Utility Software II lab 1 Jacek Wiślicki, jacenty@kis.p.lodz.pl original material by Hubert Kołodziejski

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

Arithmetic and Algebra of Matrices

2. Basic Relational Data Model

CSI 333 Lecture 1 Number Systems

SOLVING LINEAR SYSTEMS

Math Common Core Sampler Test

but never to show their absence Edsger W. Dijkstra

Introduction to Data Structures

Oracle 10g PL/SQL Training

Chapter 5 Names, Bindings, Type Checking, and Scopes

Unit 18 Determinants

Section 1.4 Place Value Systems of Numeration in Other Bases

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Oracle Database 10g: Introduction to SQL

MECHANICAL PRINCIPLES HNC/D MOMENTS OF AREA. Define and calculate 1st. moments of areas. Define and calculate 2nd moments of areas.

Multiple regression - Matrices

Chapter 7D The Java Virtual Machine

Chapter 7 Memory Management

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Instruction Set Architecture (ISA)

Patterns in Pascal s Triangle

McGraw-Hill The McGraw-Hill Companies, Inc.,

CS 106 Introduction to Computer Science I

PES Institute of Technology-BSC QUESTION BANK

State of Stress at Point

Embedded SQL programming

ML for the Working Programmer

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Chapter 3: Restricted Structures Page 1

The Crescent Primary School Calculation Policy

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Numbering Systems. InThisAppendix...

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Matrix Differentiation

Variables, Constants, and Data Types

Data Structures using OOP C++ Lecture 1

Computational Mathematics with Python

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

The Basics of FEA Procedure

Answers to Review Questions Chapter 7

Here are some examples of combining elements and the operations used:

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Cryptography and Network Security. Prof. D. Mukhopadhyay. Department of Computer Science and Engineering. Indian Institute of Technology, Kharagpur

Chapter 3. Distribution Problems. 3.1 The idea of a distribution The twenty-fold way

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

ECE 842 Report Implementation of Elliptic Curve Cryptography

Geodatabase Programming with SQL

Using Casio Graphics Calculators

Pseudo code Tutorial and Exercises Teacher s Version

Data Corruption In Storage Stack - Review

Symbol Tables. Introduction

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

3.GETTING STARTED WITH ORACLE8i

Transcription:

1 Array Abstract Data Type Table of Contents What this module is about... 1 Introduction... 2 Data type objects... 3 Array elements... 3.1 Sub -arrays... 3.2 Array descriptor... 3.3 Data type operations... 4 Enquiry operations... 4.1 Read operations... 4.2 Write operations... 4.3 Sparse array operations... 4.4 Array memory allocation methods... 5 Row and column storage... 5.1 General formula to compute the address of an array element... 5.2 Sparse arrays... 5.3 1 What this module is about This module contains a description of the array data type and some discussion as to how we allocate memory space to arrays. The value of defining arrays as an abstract data type is primarily for systems programmers, who work behind the scenes and bring you all the wonderful software that comes with an operating system, such as compilers, linkers, file managers, text editors, etc. For most of us mortal people, we simply use arrays in our programming languages without thought of the more abstract nature of arrays. We do not go into detailed program implementations. 2 Introduction The array data type is the simplest structured data type. It is such a useful data type because it gives you, as a programmer, the ability to assign a single name to a homogeneous collection of instances of one abstract data type and provide integer names for the individual elements of the collection. Figure 1 shows an array[1..7] of COLOUR. You, the programmer, can then compute the name of a specific element and avoid the need for explicitly declaring a unique name for each array element. Consider how difficult it would be to program if you had to declare explicitly a separate name to each array element and use such names in your programs. 1 2 3 4 5 6 7 Figure 1: A seven element array of COLOUR. Because the names of array elements are computed, the computer requires a means of finding! at execution time! the physical storage location corresponding to an array element from the

2 Array ADT index, or name, of the array element. As a consequence, not only do we require space for the array elements themselves, but we also require space for an array descriptor which is used at execution time to map array indices to memory addresses. The array descriptor contains the size of the array elements, the number of dimensions and bounds of each dimension. NOTE: In fact, for all data types beyond the primitive data types we require data descriptors. One could even define the primitive data types as those data types which do not require data descriptors. If you reflect on the declaration section of any programming language, you will find that the non primitive data types have complex definitional parameters. These definitional parameters correspond to the information required in the data descriptors. To make it easy to compute the memory address of an array element and to make memory management simple, we have the restriction that each element of the array must be of the same type, so that each array element can be allocated a pre defined and equal amount of memory space. Multi dimensional arrays are defined as multiple levels of one dimensional arrays. For example, the following definition of the three dimensional array of colours array[1.. 5 ; -2.. +6 ; -10.. -1] of COLOUR actually means array[1.. 5] of array[-2.. +6] of array[-10.. -1] of COLOUR 3 Data type objects The entire array is a single entity, represented by its name. For example in the following, the name chess_board refers to the entire array. chess_board : array[1.. 8, 1.. 8] of SQUARE 3.1 Array elements Each array element is a separate object. Array elements are represented by the array name plus a set of subscripts. Each subscript is in turn represented by an arithmetic expression. For example, we could have a two dimensional array called chess_board and we could refer to one of its elements! one of the squares on the chess board! using the following notation. chess_board[row-1, column+2] Another example is the the postion of an airplane over time. This requires three space coordinates, latitude, longitude and height, and one time coordinate. plane_position[its_latitude, its_longtidue, its_height, universal_time] 3.2 Sub -arrays If a subscript expression represents a range of subscript values then we consider the corresponding sub array as individual object. The structure, or shape, of the sub array depends upon what ranges we permit subscript ranges to have. The following shows some examples. 1. chess_board[*, 3]! refers to the sub array of all squares in column 3. 2. chess_board[2.. 6, 3]! refers to the sub array of squares in rows 2, 3, 4, 5 and 6 and in column 3.

Fundamentals of Data Structures 3 3. chess_board[2.. 6, 3.. 5]! refers to the sub array of squares in rows 2, 3, 4, 5 and 6, and in columns 3, 4 and 5. 3.3 Array descriptor An array also has associated with it an array descriptor. The array descriptor consists of the following objects! see Figure 2. 1. The address, A 0, of the array element which represents the location of the array element with all subscripts equal to zero. This may be a physically non existent location in main memory because arrays may be defined with arbitrary upper and lower bounds. Users of arrays do not see this object. We include it because it is required for the complete understanding of how array element addresses are computed. 2. The number of dimensions, dim_count, of the array. 3. A descriptor triple for each dimension of the array. LBi! the lower bound of dimension i UBi! the upper bound of dimension i SZi! the size (space requirement) for dimension i A " dim/count L$ 1 )$ 1 S+ 1 L$ 2 )$ 2 S+ 2......... L$ dc )$ dc S+ dc dc 5 dim/count Figure 2: A prototypical array descriptor. 4 Data type operations The operations on arrays are described in this module as procedure and function calls. However, because the array data type is built into most programming languages, a different special purpose syntax is used when arrays and array elements are referenced! see the sections following the section Data type objects. 4.1 Enquiry operations These operations retrieve information from the array descriptor. 4.1.1 How many dimensions does an array have? dimensions ( an_array : ARRAY ) : INTEGER

4 Array ADT. ensure Result = dim_count. 4.1.2 What is the lower bound of a given dimension of an array? lower_bound ( an_array : ARRAY ; dimension : INTEGER ) : INTEGER and 1 # dimension # dim_count ensure Result = LBdimension. 4.1.3 What is the upper bound of a given dimension of an array? upper_bound ( an_array : ARRAY ; dimension : INTEGER ) : INTEGER and 1 # dimension # dim_count ensure Result = UBdimension. 4.1.4 What is the amount of space used by an array element of a given dimension? size_of_element ( an_array : ARRAY ; dimension : INTEGER ) : INTEGER and 1 # dimension # dim_count ensure Result = SZdimension. 4.2 Read operations Only one function is necessary. 4.2.1 What is the address of a specifically indexed array element? index ( an_array : ARRAY ; index 1,!, indexdim_count : INTEGER ) : REFERENCE $ i : 1.. dim_count " LBi # indexi and indexi # UBi ensure Result = location_of(an_array[ index 1,!, indexdim_count ]). The function returns the address of the array element with the specified index set. We represent the address as a reference to a memory location. 4.3 Write operations The only operations are to be able to create a new array and dispose of an existing array. 4.3.1 Create a new array create_array ( an_array : NAME ; dim_count, basic_size, LB 1,!, LBdim_count, UB 1,!, UBdim_count : INTEGER) : REFERENCE

Fundamentals of Data Structures 5 ensure Result = address of an_array that is an array with the specified number of dimensions and with the specified lower and upoer bounds for each dimension. 4.3.2 Dispose of an existing array delete_array ( an_array : ARRAY ) ensure an_array is no longer a valid name as the array is removed from the environment. 4.4 Sparse array operations Sparse arrays are arrays which have mostly undefined or zero elements. Special storage techniques are used to save space by not reserving space for the unused array elements. Sparse arrays require two special operations. 4.4.1 Add an element to a sparse array add_array_element ( array_name : ARRAY, value : VALUE, index 1,!, indexdim_count : INTEGER ) $ i : 1.. dim_count " LBi # indexi and indexi # UBi value is a valid array value ensure The value is inserted into the array at the specified index position. If the element is already in the array the value will be changed. 4.4.2 Delete an element from a sparse array delete_array_element ( array_name : ARRAY, index 1,!, indexdim_count : INTEGER ) $ i : 1.. dim_count " LBi # indexi and indexi # UBi ensure The appropriate array element is removed from the table. 5 Array memory allocation methods In this module, we only describe, in more detail, row and column order memory allocation and sparse matrix memory allocation. We do not describe in detail memory allocation methods for other special cases such as triangluar arrays (values above or below the main diagonal are all zero) and symmetric arrays (where the relationship A[i,j] = A[j,i] holds). In the last two cases, it is wasteful to store all those zero or repetative array values. 5.1 Row and column storage This is the most common method of allocating memory space to an array. With this method we assume that all of the array elements are to be equally accessible at all times.

6 Array ADT The first step is to compute the total space required by all the array elements and allocate one contiguous chunk of memory big enough to contain all the array elements. The starting address for the allocated memory is As. The second step is to assign array elements to a sequence of memory locations. The method chosen must have the property that an efficient index function can be created to compute quickly the memory location of any array element from a given set of indices. The assignment of array elements to memory locations is done by systematically varying the index values. We treat each index position as a digit in a mixed radix numbering scheme where the digit values in each position vary between the lower and upper bounds associated with the index position. The systematic variation is to "add one" to a given index set to obtain the next array element in sequence. NOTE: In decimal notation, each digit position in a number is assigned the same range of digit values, 0 to 9. In array storage allocation each digit position has its own range of digit values. We now have two choices as to how to "add one". We can add at the right most index position and propagate carries to the left. Or, we can add at the left most index position and propagate carries to the right. If the right most subscript changes faster, we have row order storage! used in C and Java. If the left most subscript changes faster, we have column order storage! used in Fortran. The terminology arises from the pictorial representation of storing a two dimensional array. 1"1 1"2 1"3 2"1 2"2 2"3 3&1 3"2 3"3 4"1 4"2 4"3 Figure 3: A 4x3 two dimensional array. row 1 row 2 row 3 row 4... 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 4,1 4,2 4,3... Figure 4: Row order storage for the array in Figure 3. column 1 column 2 column 3... 1,1 2,1 3,1 4,1 1,2 2,2 3,2 4,2 1,3 2,3 3,3 4,3... Figure 5: Column order storage for the array in Figure 3.

Fundamentals of Data Structures 7 5.2 General formula to compute the address of an array element The formula, F1, is used to compute the address of an array element. (F1) As + dc % (Ik! LBk) # SZk where dc = dim_count As = address of the first array element The calculation of the size, SZk, of the elements in each dimension of the array is done once at the time the array is created and is stored in the array descriptor. The recurrence relation (R1) is used to compute the values of SZk. (R1) SZdim_count = the size of the base type for the array SZk = SZk+1 # (UBk+1! LBk+1 + 1) $k : dim_count! 1! 1 NOTE: If, for the computation, we number the dimensions in A[*,!, *] from left to right we have row order storage. If, for the computation, we number the dimensions from right to left we have column order storage. The following arithmetic relationship, R2, is true. (R2) dc % (Ik! LBk) # SZk = dc % Ik # SZk! dc % LBk # SZk We substitute the right hand side of R2 into the formula F1 to obtain the formula F2. (F2) As + dc % Ik # SZk! dc % LBk # SZk Note that As and % dc LBk # SZk are constants, so we can define the following relationship R3. A 0 is the memory address where the array element with all zero indicies would be stored. (R3) A 0 = As! % dc LBk # SZk A 0 is a constant that only needs to be computed once at the time the array is created. As a consequence, the value of A 0 is stored in the array descriptor and we can compute the address of an array element using the formula F3. (F3) A 0 + dc % Ik # SZk Example 1: Figure 6 shows an example definition for a four dimensional array and its corresponding descriptor that corresponds to row order storage. The arithmetic terms are left unsimplifed to show how the formulas are applied.

8 Array ADT 1000 ( (3 * 5*6*7*n) +( 2 * 6*7*n) +(0 * 7*n) +( 6 * n) ) 4 array[3..6, -2..+2, 0..5, -6..0] of T ----8 array [3..6] of array [-2..+2] of array [0..5] of array [-6..0] of T 1 3 6 5 * 6*7*n T, the base type, is of size n bytes. 2 2 +2 6 * 7*n A s is 1000 3 4 0 5 7 * n 6 0 n LB UB SZ Figure 6: An example array descriptor for a 4-dimensional array 5.3 Sparse arrays A special case occurs when most of the array elements are zero or undefined. In such cases, we do not want to store all the zero elements. There are various schemes used to store such arrays which make efficient use of memory. What all the methods have in common is that extra information must be stored that maps array indices to memory locations. A table, called the index value table, is formed which stores the index values for the non zero array elements together with the array element value corresponding to the index set. When a reference is made to an array element, the indices are compared with those in the index value table until a match is found and the array element value is returned, or, if there is no match, and a zero value is returned. The differences among ways of storing sparse arrays occur in the method used to store the index value table. The table could be implemented using arrays or, as is also common, using a multi list structure.