Multidimensional Arrays - Fortran Programming - Lecture Notes, Study notes of Computer Programming

These study notes are very easy to understand and very helpful to built a concept about the foundation of computers.The key points discuss in these notes are:Multidimensional Arrays, Multiple Dimensions, Upper Bound, Array References, Multiple Subscripts, Row Major Order, Explicit and Implied, Column Major Order, Processing Entire Array, Array Storage Requirements

Typology: Study notes

2012/2013

Uploaded on 04/27/2013

ashalata
ashalata 🇮🇳

3.8

(18)

106 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Multidimensional Arrays
Multidimensional Arrays
Arrays may be declared with multiple dimensions (up to seven).
Each dimension's bound is specified either by a single number, the
size, or by a pair of numbers, the lower bound and the upper bound.
Example: Consider A 4 × 5 matrix "INFO" of integers:
Columns
INFO
1
2
3
4
5
1
5
23
16
7
54
Rows
2
42
17
33
40
34
3
11
32
81
43
12
4
8
5
12
29
72
INTEGER INFO(4, 5)
...
INFO(2, 4) has value 40
INFO(1, 1) has value 5
INFO(4, 3) has value 12
Array References - In order to reference an element of a
multidimensional array it is necessary to have multiple subscripts,
one for each dimension of the array.
array-name ( subscript-1, subscript-2, . . . )
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Multidimensional Arrays - Fortran Programming - Lecture Notes and more Study notes Computer Programming in PDF only on Docsity!

Multidimensional Arrays

Multidimensional Arrays

  • Arrays may be declared with multiple dimensions (up to seven).
  • Each dimension's bound is specified either by a single number, the size, or by a pair of numbers, the lower bound and the upper bound.
  • Example: Consider A 4 × 5 matrix "INFO" of integers: Columns INFO 1 2 3 4 5 1 5 23 16 7 54 Rows 2 42 17 33 40 34 3 11 32 81 43 12 4 8 5 12 29 72

INTEGER INFO(4, 5) ... INFO(2, 4) has value 40 INFO(1, 1) has value 5 INFO(4, 3) has value 12

- Array References - In order to reference an element of a multidimensional array it is necessary to have multiple subscripts, one for each dimension of the array.

array-name ( subscript-1, subscript-2,... )

  • More examples: REAL TABLE(6, 3, 0:5) CHARACTER*30 NAMES(10,20) TABLE(3, 2, 0) = 45. NAMES(3, 6) = "Jack P. Walford" - Row Major Order - When an array is processed one row at a time using explicit or implied DO loops, it is being processed in row major order.

INTEGER INFO(4, 5), ROW, COL DO 20 ROW = 1, 4 DO 10 COL = 1, 5 PRINT *, INFO(ROW, COL) 10 CONTINUE 20 CONTINUE

DO 20 ROW = 1, 4 PRINT 10, (INFO(ROW, COL), COL = 1, 5) 10 FORMAT (1X, (6X, I3)) 20 CONTINUE

- Processing an Entire Array - an array name may be used without a subscript in an input / output list and in a DATA statement.

INTEGER TABLE(5,3), ROW, COL PRINT *, 'Enter 15 values, one column at a time' READ *, TABLE

  • All values stored by default in column major order.
  • When a multidimensional array is used in this manner the elements are processed in column major order, that is with the first subscript being varied the fastest.

INTEGER TABLE(5,3), ROW, COL C Initialize TABLE to all ones DATA TABLE /15 * 1/ C Store in column major order DATA TABLE /2, 4, 6, 8, 10, 12, 14, 16, 18,

  • 20, 22, 24, 26, 28, 30/ C Store in Row major order DATA ((TABLE(I, J), J = 1, 3), I = 1, 5)
  • /2, 4, 6, 8, 10, 12, 14, 16, 18,
  • 20, 22, 24, 26, 28, 30/

- Array Storage Requirements - The total number of elements in an array is determined by multiplying the sizes of its various dimensions together. - The size of a dimension is equal to the upper bound minus the lower bound plus one. - The following array has a total of 22050 elements.

INTEGER SCORES ( -10:10, 50, 0:20)

(10 - (-10) + 1) * 50 * (20 - 0 + 1) = 21 * 50 * 21 = 22050

- Using a Multidimensional Array - When declaring a multidimensional array, it is important to associate a meaning or purpose with each dimension of the array. - The order in which the dimensions are declared is of little importance. (There may, however, be performance issues that are significant!) - Using a Multidimensional Array as a Dummy Argument - When a dummy argument is used to pass a multidimensional array to a subroutine (or function), only the name of the dummy array argument appears in the dummy argument list. - The size of the dummy array argument is determined in a separate declaration statement in the subroutine (or function). - For a multidimensional array, only the last dimension may be specified by using an asterisk ; however, all of the other dimensions must be supplied as constants or other dummy arguments.

SUBROUTINE name (... array-name , argument-n ,... ) type array-name ( argument-n , * )

Case Study: Image Enhancement

A secret military satellite has taken a digital image of a foreign military base. Information is in a matrix of integers, where each entry is a pixel. Each pixel has a brightness, specified from 0 to 9. The image is hard to interpret for two reasons:

  • The signal way jammed, making it noisy.
  • The signal contrast needs to be improved

Image Cleaning : removing noise

  • A pixel greatly different from its neighbors is assumed to be “noise”.
  • Noise - different from all eight neighbors by 3 or more.
  • Noisy pixel becomes the average of all eight neighbors.
  • Don't clean edge values. Before After 3 3 1 4 5 3 3 1 4 5 2 3 6 4 6 2 3 6 4 6 1 5 5 6 5 → 1 5 5 6 5 5 6 5 1 6 5 6 5 5 6 4 4 4 5 4 4 4 4 5 4

pixel(4,4): (5 + 6 + 5 + 5 + 6 + 4 + 5 + 4) / 8 = 5.

Image Contrast:

  • Increased contrast makes major detail easier to see
  • Greater contrast - fewer number of pixel values
  • Reduce to three distinct values: 0, 1, 4
  • 0, 1, 2, 3 → 0
  • 4, 5, 6 → 1
  • 7, 8, 9 → 4 3 3 1 4 5 0 0 0 1 1 2 3 6 4 6 0 0 1 1 1 1 5 5 6 5 → 0 1 1 1 1 5 6 5 5 6 1 1 1 1 1 4 4 4 5 4 1 1 1 1 1

SUBROUTINE INIMAG (IMAGE, MAXROW, MAXCOL) C Reads the image.

C Input Arguments. C MAXROW - Number of rows in IMAGE. C MAXCOL - Number of columns in IMAGE.

C Output Arguments. C IMAGE - The array of pixels.

C Argument Declarations. INTEGER MAXROW, MAXCOL INTEGER IMAGE(MAXROW, MAXCOL)

C Local Declarations. INTEGER I, J

C Read each row of data until done. OPEN (UNIT = 2, FILE = 'DIGITAL.DAT', STATUS = 'OLD') DO 10 I = 1, MAXROW READ (2, 15) (IMAGE(I, J), J = 1, MAXCOL) 15 FORMAT (78I1) 10 CONTINUE

C Exit subroutine. RETURN END

SUBROUTINE CLEAN (IMAGE, MAXROW, MAXCOL) C Finds noisy pixels and substitutes neighbors' average value.

C Input Arguments. C MAXROW - Number of rows in IMAGE. C MAXCOL - Number of columns in IMAGE.

C Input/Output Arguments. C IMAGE - The array of pixels.

C Argument Declarations. INTEGER MAXROW, MAXCOL INTEGER IMAGE(MAXROW, MAXCOL)

C Local Declarations. INTEGER NOSLEV PARAMETER (NOSLEV = 3) INTEGER CELL INTEGER I, J LOGICAL NOISY

C Check for noise and replace noisy pixels. DO 10 I = 2, MAXROW- DO 20 J = 2, MAXCOL- CELL = IMAGE(I,J) NOISY = (IABS(CELL - IMAGE(I-1,J-1)) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I-1,J )) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I-1,J+1)) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I ,J-1)) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I ,J+1)) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I+1,J-1)) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I+1,J )) .GE. NOSLEV) +.AND. (IABS(CELL - IMAGE(I+1,J+1)) .GE. NOSLEV)

SUBROUTINE REDUCE (IMAGE, MAXROW, MAXCOL) C Replaces each of the symbols in IMAGE with a standard symbol.

C Input Arguments. C MAXROW - Number of rows in IMAGE. C MAXCOL - Number of columns in IMAGE.

C Input/Output Arguments. C IMAGE - The array of pixels.

C Argument Declarations. INTEGER MAXROW, MAXCOL INTEGER IMAGE(MAXROW, MAXCOL)

C Local Declarations. INTEGER SYMBL1, SYMBL2, SYMBL PARAMETER (SYMBL1 = 0, SYMBL2 = 1, SYMBL3 = 4) INTEGER I, J

C Perform the replacement. DO 10 I = 1, MAXROW DO 20 J = 1, MAXCOL IF (IMAGE(I,J) .LE. 3) THEN IMAGE(I,J) = SYMBL ELSE IF (IMAGE(I,J) .LE. 6) THEN IMAGE(I,J) = SYMBL ELSE IMAGE(I,J) = SYMBL END IF 20 CONTINUE 10 CONTINUE

C Exit subroutine. RETURN END

SUBROUTINE SHIMAG (IMAGE, MAXROW, MAXCOL) C Displays the enhanced image.

C Input Arguments. C MAXROW - Number of rows in IMAGE. C MAXCOL - Number of columns in IMAGE.

C Input/Output Argument. C IMAGE - The array of pixels.

C Argument Declarations. INTEGER MAXROW, MAXCOL INTEGER IMAGE(MAXROW, MAXCOL)

C Local Declarations. INTEGER I, J

C Display each row of the image. DO 10 I = 1, MAXROW PRINT 15, (IMAGE(I,J), J = 1, MAXCOL) 15 FORMAT (1X, 78I1) 10 CONTINUE

C Exit subroutine. RETURN END

Sorting an Array - Bubble Sort

SUBROUTINE BUBBLE (LIST, N) C Sorts the data in array LIST. C Input Argument. C N - Number of array elements to be sorted. C Input/Output Argument. C LIST - Array being sorted. C Argument Declarations. INTEGER LIST(*), N C Local Declarations. INTEGER FIRST, PASS, TEMP LOGICAL SORTED C Start with pass 1. PASS = 1 C Exchange out of order pairs while array is not sorted. SORTED = .FALSE. DO WHILE (.NOT. SORTED) SORTED = .TRUE. DO 10 FIRST = 1, N-PASS IF (LIST(FIRST) .GT. LIST(FIRST+1)) THEN TEMP = LIST(FIRST) LIST(FIRST) = LIST(FIRST+1) LIST(FIRST+1) = TEMP SORTED = .FALSE. END IF 10 CONTINUE PASS = PASS + 1 END DO

C Exit subroutine. RETURN END

Sorting an Array - Insertion Sort

SUBROUTINE INSRTN (LIST, N) C Sort the array LIST using the insertion sort algorithm C Precondition: LIST must be an INTEGER array with subscripts form 0 to N and with the 0 th element being unused (available). C Postcondition: The first through the N th^ elements of array LIST will be in ascending order. The 0 th^ element of List is undefined. C Dummy Arguments INTEGER LIST( 0 : * ), N C Local Variables INTEGER I, K C Process the elements of LIST one-by-one from the second element through the n-th element DO 10 I = 2, N C Move the I-th element of LIST to LIST(0) LIST (0) = LIST (I) C Move those values of the partially sorted array C LIST which are greater than LIST (0) "right" one C in the array. The search is from I-1 to 0. K = I - 1 DO WHILE (LIST(K) .GT. LIST(0) ) LIST(K + 1) = LIST(K) K = K - 1 END DO C Insert LIST(0) back into the array LIST at its C proper place among the first I elements of LIST LIST(K + 1) = LIST(0) 10 CONTINUE RETURN END

K = N DO WHILE (LIST(K) .GT. LIST(0) ) LIST (K + 1) = LIST(K) K = K - 1 END DO

C Insert the value just read into the array LIST at its C proper place amount the first N elements and increase N

LIST(K + 1) = LIST(0) 10 CONTINUE READ ( *, *, END = 20 ) LIST(0) PRINT *, 'The array LIST in INSRTN is full!' PRINT *, 'Data has been lost!'

C Set N 20 N = J - 1

RETURN END

Using Subprograms in a Large-Scale Problem

The Implied DO Loop: The implied DO loop is a very useful tool. Its use is not limited to printing and reading arrays.

The following statement will print the number 1 to N.

PRINT format, (I, I - 1, N)

The following statement will print N stars.

PRINT format, ('*', I = 1, N)

This feature is used in the bar graph (histogram) subroutine, BRGRPH, in the grader program in the textbook.