CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
29 April, 2000 CS1001 Lecture 26 Static vs Dynamic Data Structures Array vs Link List Pointers Link List Operations Example.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Figure 6.1 IEEE floating-point formats: (a)
Two Dimensional Arrays. One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3.
Chapter 8 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
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.
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
LAB-12 2-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
Multi-Dimensional Arrays
Chapter 8 Arrays and Strings
Arrays- Part 2 Spring 2013Programming and Data Structure1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
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.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
An introduction to arrays WEEK 7 Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values,
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
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.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 4 Section 1 Organizing Data into Matrices.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
CSC 201 Computer Programming- FORTRAN
Programming For Nuclear Engineers Lecture 6 Arrays
CHAPTER 8 ONE-DIMENSIONAL ARRAYS
Array processing and Matrix manipulation
JavaScript: Functions.
Introduction To Programming Information Technology , 1’st Semester
Array processing and Matrix manipulation
Presentation transcript:

CHAPTER 9 MUTIDIMENSIONAL ARRAYS

Introduction to multidimensional Arrays and Multiply subscripted variables

Compile-Time Arrays & Run-Time Arrays Compile-Time Arrays: The size is fixed before execution begins. Run-Time (or Allocatable 可分配的 ) Arrays: The memory is allocated ( 分配 ) during execution, making it possible to allocate an array of appropriate size.

Compile-Time Arrays REAL, DIMENSION(4, 3)::Temperature REAL, DIMENSION(1:4, 1:3):: Temperature Temperature (2, 3 ) 64.5 Temperature (I, J) REAL, DIMENSION(4, 3, 7):: TemperatureArray REAL, DIMENSION(1:4, 1:3, 1:7)::TemperatureArray TemperatureArray (1, 3, 2) → 64.3 TemperatureArray (Time, Location, Day) →

Compile-Time Arrays REAL, DIMENSION(1:2, -1:3)::Gamma Gamma(1, -1), Gamma(1,0), Gamma(1,1), Gamma(1, 2), Gamma(1,3), Gamma(2, -1), Gamma(2,0), Gamma(2,1), Gamma(2, 2), Gamma(2,3) REAL, DIMENSION (0:2, 0:3, 1:2) :: Beta INTEGER, DIMENSION(5:12) :: Kappa

Declaration of Compile-Time Array type, DIMENSION(l 1 :u 1, l 2 :u 2, ‧ ‧ ‧ l k :u k ) :: & list-of-array-names l i :u i The specified lower limit l i through the upper limit u i. The number k of dimensions, called the rank ( 秩 ) of array, is at most seven.

Declaration of Allocatable Array type, DIMENSION(:, :, ‧ ‧ ‧ :), & ALLOCATABLE :: list type, DIMENSION(:, :, ‧ ‧ ‧ :) :: list ALLOCATABLE :: list The rank k of the array (the number of dimensions) is at most seven.

Allocatable Array ( 可分配的 ) / Run-Time Arrays REAL, DIMENSION(:, :, :), ALLOCATABLE :: & Beta REAL, DIMENSION(:, :), ALLOCATABLE :: & Gamma

ALLOCATE Statement ALLOCATE (list) ALLOCATE (list, STAT = status-variable) where list is a list of array specifications of the form array-name (l 1 :u 1, l 2 :u 2, ‧ ‧ ‧ l k :u k ) ALLOCATE (Beta(0:2, 0:3, 1:2), Gamma & (1:N, -1:3), STAT = AllocateSatus) DEALLOCATE(***)

Input/Output of Multidimensional Arrays Element-wise Processing row ( 列 ) × column ( 行 ) Two natural orders for processing the elements of a two-dimensional array: row-wise and column-wise. In most cases, a programmer can select one of these orderings by controlling the way the subscripts ( 下標 ) vary. If this is not done, the Fortran convention is that two-dimensional arrays will be processed column-wise.

(a) Row-wise Processing (b) Column-wise Processing

Processing a Three-Dimensional Array (2 × 4 × 3)

Input/Output of Array Elements Using a DO loop Using the array name Using an implied DO loop Using an array section INTEGER, DIMENSION (3, 4) :: Table

Input/Output Using DO Loops INTEGER, DIMENSION (3, 4) :: Table DO Row = 1, 3 DO Col =1, 4 READ *, Table (Row, Col) END DO

Input/Output Using DO Loops INTEGER, DIMENSION (3, 4) :: Table DO Col =1, 4 DO Row = 1, 3 READ *, Table (Row, Col) END DO DO Row = 1, 3 DO Col =1, 4 PRINT *, Table (Row, Col) END DO

Input/Output Using the Array Name INTEGER, DIMENSION (3, 4) :: Table READ *, Table 77, 99, 48, 56, 10, 89 32, 100, 77, 25, 46, 33 PRINT ‘ (1X, 4I5/) ’ Table

Input/Output Using Implied DO Loops INTEGER, DIMENSION (3, 4) :: Table READ *, ((Table (Row, Col), Col =1, 4 ), & Row = 1, 3) READ *, (Table (Row,1), Table (Row,2), & Table (Row,3), Table (Row,4), & Row = 1, 3)

Input/Output Using Implied DO Loops READ *, ((Table (Row, Col), Row = 1, 3) ), & Col =1, 4) READ *, (((B(I, J, K), I = 1, 2), J =1, 4), & K = 1, 3)

Input/Output Using Implied DO Loops DO Row = 1, 3 PRINT ‘ (1X, 4I5) ’, (Table (Row, Col), Col =1, 4) END DO

Examples Figure 9.3, p.628 Temperature Table Rate is a 3 × 4 array

Examples: p. 630 READ *, N, (Number (I), I =1, N), M, & ((Rate (I,J), J = 1, N), I = 1, M) 4 16, 37, 76, , 7.3, 18.4, , 1.0, 1.0, , 16.9, 0.0, 0.0

Examples: p. 630 PRINT 5, ( “ Row ”, I, (Rate (I,J), J= 1, 4), I = 1, 3) 5 FORMAT (1X, A, I2, “ -- ”, 4F6.1/) Row _________________________ Row _________________________ Row _________________________

Examples: p. 630 PRINT 6, (J, (Rate (I,J), I = 1, 3), & Number (J), J= 1, 4), “ Total ”, Total 6 FORMAT (4(1X, I4, 5X, 3F6.1, I10/), A, T35, I3) Total 152

9.3 Processing Multidimensional Arrays Array Constants INTEGER, DIMENSION (2, 3) :: A A = RESHAPE ((/ 11, 22, 33, 44, 55, 66 /), (/ 2, 3 /)) or A = RESHAPE ((/ (11*N, N =1, 6) /), (/ 2, 3 /) Reshape (v.) 重塑 Shape (n. v.) 形狀

Array Constants A = RESHAPE ((/11, 22, 33, 44, 55, 66 /), & (/ 2, 3 /), ORDER = (/2, 1/)) The order (/2, 1/) specifies that the second subscript ( 下標 ) is to be varied before the first, which causes the array to be filled row-wise.

Array Constants A = RESHAPE ((/11, 22, 33, 44 /), (/ 2, 3 /), & PAD = (/0, 0/), ORDER = (/2, 1)) pad (v.) 填充

Array Constants The intrinsic function SHAPE can be used to determine the shape of an array, which consists of number of dimensions for array and the extent (the number of subscripts 下標 之大小程度 ) in each dimension. For example, SHAPE (A) will return (2, 3). Shape 形狀 (n.); 塑造 (v.)

Array Expressions (p. 636) & Array sections and Subarrays INTEGER, DIMENSION (2, 3) :: A A(1:2:1, 2:3:1) or A(:, 2:3)

Array sections and Subarrays A(2, 1:3:1) or A(2, :) A((/ 2, 1 /), 2:3)

Array Assignment INTEGER, DIMENSION (2, 3) :: A INTEGER, DIMENSION (3, 2) :: B A = 0 B = RESHAPE (A, (/3, 2/))

Array Assignment A(:, 2:3) = RESHAPE ((/ (I**2, I = 1, 4) /), & (/2, 3/))

Array Assignment: Example REAL, DIMENSION (2, 3) :: Alpha, Beta WHERE (Alpha /= 0.0) Beta = 1.0 / Alpha ELSEWHERE Beta = 0.0 END WHERE

Intrinsic Array-Processing Subprograms

Matrix Processing (Sec. 9.6) & Intrinsic Array-Processing Subprograms MATMUL (A, B) --- The product AB TRANSPOSE (A)

Application: Pollution Tables In a certain city, the air pollution is measured at a two-hour intervals, beginning at midnight. These measurements are recorded for a one- week period and stored in a file, the first line of which contains the pollution level for day 1, the second line for day 2, and so on. A program must be written to produce a weekly report that displays the pollution levels in a table of the form:

Monitoring Air Pollution