CS1001 Lecture 19 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001 Lecture 19

Description:

Compile-time and Run-time arrays. Compile-Time arrays ... allocate fixed number of spaces for array at compiling time. Two problems ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 8
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: CS1001 Lecture 19


1
CS1001 Lecture 19
  • Some More Arrays Processing
  • Application of Arrays
  • Stacks
  • handouts StackTest and QueueTest

2
STOP Statement
  • END PROGRAM program_name
  • last statement of every program
  • indicates to the compiler the end of the program
  • halts the execution of the program
  • STOP or STOP constant
  • terminates executive of the program
  • in the second form
  • constant is an integer constant or a character
    string
  • constant is printed out at termination by that
    STOP

3
Compile-time and Run-time arrays
  • Compile-Time arrays
  • type, DIMENSION (lu) list _of_array_names
  • l is the lower limit of the array, u is the upper
    limit of the array
  • allocate fixed number of spaces for array at
    compiling time
  • Two problems
  • waste of space when too much is allocated
  • possibility of array overflow is not enough is
    allocated
  • Allocatable or run-time array
  • memory is allocated during run-time
  • Declare arrays to be allocatable,
  • allocate the required space,
  • and if needed, deallocate the space.

4
Run-time array
  • Declaration
  • type, DIMENSION ( ), ALLOCATABLE list
  • or
  • type, DIMENSION ( ) List
  • ALLOCATABLE list
  • E.g., REAL, DIMENSION ( ), Allocatable A,B
  • Allocate Space
  • ALLOCATE (list)
  • or
  • ALLOCATE(list, STATstatus_variable)
  • E.g. PRINT , enter size of arrays A
    and B
  • READ , N
  • ALLLOCATE( A(N), B(0N1), STAT status)
  • IF (status .NE. 0) STOP Not enough memory

Status_variable set to 0 if operation is
successful
5
Run-time array
  • Deallocate Space
  • DEALLOCATE (list)
  • or
  • DEALLOCATE(list, STATstatus_variable)
  • status_variable will be set to zero if
    operation is successful

6
Arrays as arguments
  • Intrinsic functions (page 548 and Appendix D)
  • MAXVAL(A) returns the maximum value of array A
  • MINVAL(A) returns the minimum value of array A
  • SIZE(A) returns the number of elements in array
    A
  • SUM(A) returns the sum of the elements of array A
  • User-Defined subprogram
  • actual array argument must be declared in calling
    program unit
  • corresponding formal array argument must be
    declared in the called subprogram unit

INTEGER, DIMENSION( ) ALLOCATABLE Stack .
. . Call PUSH (Stack) . . . SUBROUTINE
Push(Stack) INTEGER, DIMENSION ( ),
INTENT(INOUT) Stack
Calling unit
Callee unit
7
Stack
  • A data structure used to implement
    Last-in-first-out (LIFO) processes
  • e.g. stack of trays in the cafeteria

  • Two operations
  • Push
  • Pop
  • Some design issues
  • How long (or deep) should the stack be (i.e.,
    the dimension of the stack)?
  • When push, is there room in array?
  • When pop, is the stack empty ?

Top
Stack(top)

Stack(4)
Stack(3)
Stack(2)
Stack(1)
Stack
Write a Comment
User Comments (0)
About PowerShow.com