CS117 Programming Methodology
Fall 1999

Simulating code by hand: Fortran

Analyze the program on the last page and simulate the execution of the program using the pencil-and-paper method demonstrated in class. Make sure that the following are made clear in your simulation table:

  1. Each scope is contained in a column of the table, headed by the name of the function (or just global) that defines the scope. Draw vertical ines to separate the columns.
  2. Each scope column has sub-columns within it for each variable defined in the scope. Constants can be omitted.
  3. There is a column for output.
  4. If the program reads input, that the input values are made explicit in a separate section (not in the table).
  5. There is only one change of value per row in the table. i.e. each assignment changes one value only, and each row corresponds to a separate assignment.
  6. It is acceptable to ignore a scope that does not declare new variable names.
  7. Function call and return are indicated by horizontal lines in that function's scope column .

Points to watch out for

  1. Finding the end of a do loop.
  2. Finding the end of an if-then-else construct.
  3. Passing a value to a subprogram.
  4. Passing an array to a subprogram.
  5. Returning a value from a function.
  6. Local declarations within a subprogram body.

 

Due date

Hand in your completed simulation table to me (RTH) by Monday, November 15th. in class.


 

      integer function maxmin(a, t)

      integer a(3)

      integer t

      integer i, ind, val

 

      if (t .eq. 1) then

         val = 0

         do 100 i = 1,3

            if (a(i) .gt. val) then

               val = a(i)

               ind = i

            endif

 100     continue

      else

         val = 256

         do 200 i = 1, 3

            if (a(i) .lt. val) then

               val = a(i)

               ind = i

            endif

 200     continue

      endif

      maxmin = ind

      end

 

      subroutine normalize(arr, a, b)

      integer arr(3)

      integer a, b, i

 

      do 100 i = 1, 3

         arr(i) = arr(i) * b / a

 100  continue

      end

 

      program norm

      integer arr(3)

      data arr /121, 221, 56/

      integer maxValInd, minValInd, maxVal

 

      maxValInd = maxmin(arr, 1)

      maxVal = arr(maxValInd)

      call normalize(arr, maxVal, 256)

      minValInd = maxmin(arr, 2)

      print *, 'Range is ', arr(minValInd), ' to ', arr(maxValInd)

 

      end