























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























Multidimensional Arrays
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,... )
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
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,
- 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 , * )
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:
Image Cleaning : removing noise
pixel(4,4): (5 + 6 + 5 + 5 + 6 + 4 + 5 + 4) / 8 = 5.
Image Contrast:
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
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
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.