CSCE150 Fortran Lab10 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CSCE150 Fortran Lab10

Description:

8 columns. legal values of the second subscript are -1 to 6. 4. initializing arrays ... Note the default reading order is column major order. 1 2 3 4 5. 6 7 8 9 ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 17
Provided by: cse92
Category:

less

Transcript and Presenter's Notes

Title: CSCE150 Fortran Lab10


1
CSCE150 Fortran Lab10
2
outline
  • two-dimensional arrays
  • declaring arrays
  • initializing arrays
  • output
  • debugging skills
  • exercise 1
  • exercise 2
  • exercise 3

3
two-dimensional arrays
  • declaring arrays
  • declare the type and size of an array
  • Example
  • REAL, DIMENSION(3,5) box2
  • REAL, DIMENSION(3,-16) box2

1 2 3 4 5 6 7 8 9 10 11 12
13 14 15
3 rows
5 columns
8 columns legal values of the second subscript
are -1 to 6
4
  • initializing arrays
  • 3 methods to initialize
  • 1. assignment statements
  • element-by-element in a DO loop
  • DO i 1, 3
  • DO j 1, 5
  • box2(i,j) 5(i-1) j
  • END DO
  • END DO
  • use an array constructor
  • box2RESHAPE( (/1,6,11, 2,7,12, 3,8,13, 4,9,14,
    5,10,15/),(/3,5/))

3 rows
5 columns
5 columns
3 rows
column major order 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
5
  • 2. Initializing arrays in type declaration
    statements
  • REAL,DIMENSION(3,5)box2RESHAPE((/1,6,11,
    2,7,12, 3,8,13, 4,9,14, 5,10,15/),(/3,5/))
  • 3. Initializing arrays with READ statements
  • REAL,DIMENSION(3,5)box2
  • READ (,) box2

1 2 3 4 5 6 7 8 9 10 11 12
13 14 15
Note the default reading order is column major
order.
6
  • output
  • WRITE (,) box2(1,1),box2(1,2),box2(1,3),box2(1,4
    ),box2(1,5)
  • WRITE (,) ( (box2(i,j), i1,3), j1,5 )
  • WRITE (,) ( (box2(i,j), j1,5), i1,3 )
  • WRITE (,) box2(131, 151)

implied DO loop
column major order
row major order
index range from 1 to 3, step is 1
7
Debugging skills
  • Syntax errors
  • For example misspelling
  • Compiler helps a lot to find
  • Runtime errors
  • Compiler wont help
  • PROGRAM test
  • INTEGER i 2
  • INTEGER j, k
  • WRITE(,) 'Please type in a value for j'
  • READ(,) j
  • k i / j
  • WRITE(,) j
  • END PROGRAM
  • ! Divide
  • IF (j0) THEN
  • WRITE (,) ' Can not divide by zero!'
  • ELSE

If the user types in 0 for j, we will get a
floating exception.
8
Debugging skills
  • Logic errors
  • Compiler wont help
  • The most difficult errors to find
  • WRITE out the intermediate results helps a lot
  • PROGRAM test
  • REAL i 2.0
  • REAL square_root
  • square_root i (1/2)
  • WRITE(,) square root of 2 , square_root
  • END PROGRAM

9
  • PROGRAM test
  • REAL i 2.0, intermediate
  • REAL square_root
  • intermediate 1/2
  • WRITE(,) intermediate , intermediate
  • square_root i (1/2)
  • WRITE(,) square root of 2 , square_root
  • END PROGRAM

0.0
10
  • Some rules to reduce errors
  • Remember always add IMPLICIT NONE statement to
    your code
  • Remember always echo/WRITE out the input values
  • Remember Use blank lines and comments for each
    logic section
  • Remember Use indentation and parentheses to
    make statements readable
  • Remember Document your codes to improve
    readability
  • Remember You can always WRITE out the value of
    some variables to trace their variation for
    debugging purpose.

11
exercise 1
  • Write a program that gets a 3x4 two-dimensional
    array from the user and calculates the summation
    of all elements and mean.
  • example

1 2 3 4 6 7 8 9 11 12 13 14

sum90 mean7.5
12
(No Transcript)
13
exercise 2 transpose
  • Get a 3x4 integer matrix from the user, print the
    original matrix and the transpose of the matrix
    to the user.
  • example
  • 3x4 matrix transpose of the matrix

1 6 11 2 7 12 3 8 13 4
9 14
1 2 3 4 6 7 8 9 11 12 13
14
14
(No Transcript)
15
exercise 3 debugging
  • Calculate N factorial for a positive integer
    value of N
  • the integer N is inputted by a user
  • N!1 N0
  • N!N(N-1)(N-2)321 Ngt0

16
exercise 3 debugging
  • PROGRAM debugging
  • IMPLICIT NONE
  • INTEGERN, fac, i
  • WRITE(,) 'Please input an integer value of N'
  • READ(,) N
  • IF(N0) THEN
  • fac 1
  • ELSE
  • Do i1,N
  • fac fac i
  • END DO
  • END IF
  • WRITE(,) 'Factorial of N is', fac
  • END PROGRAM

Try to compile and run this program. You will
find the result factorial is always 0, no matter
what you choose for N. Debug this program to fix
logic errors.
Write a Comment
User Comments (0)
About PowerShow.com