CSCE150 Fortran Lab11 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CSCE150 Fortran Lab11

Description:

ACHAR, IACHAR, LEN, LEN_TRIM, TRIM (150-old textbook, 159-new textbook) ... ACHAR(int1): returns the character corresponding to int1 in the ASCII collating sequence ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 29
Provided by: cse92
Category:
Tags: achar | csce150 | fortran | lab11

less

Transcript and Presenter's Notes

Title: CSCE150 Fortran Lab11


1
CSCE150 Fortran Lab11
2
outline
  • recursive problems and recursive procedures
  • Fibonacci numbers
  • Tower of Hanoi
  • character manipulations
  • exercise 1
  • exercise 2

3
recursive problems
  • a recursive problem
  • the solution of the problem can be derived from
    the solutions of a smaller size of the same
    problem
  • Fibonacci numbers
  • 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, .

Fib(0)
Fib(1)
Fib(2) Fib(1) Fib(0)
4
recursive procedures
  • a recursive problem can be solved using a
    recursive procedure
  • a recursive procedure is one that invokes itself
    directly or indirectly
  • ordinary subroutines and functions in Fortran can
    not invoke themselves
  • however, by adding a special term, Fortran allows
    a procedure to be declared RECURSIVE
  • page 518 old textbook
  • page 569 new textbook

5
recursive procedures
  • RECURSIVE SUBROUTINE sub (argu_list)
  • END SUBROUTINE
  • or
  • RECURSIVE FUNCTION fun (argu_list) RESULT
    (answer)
  • END FUNCTION

6
  • when defining recursive functions
  • a special dummy argument is used whenever we want
    to specify a value to return
  • the name of this special dummy argument is
    specified in a RESULT clause in the function
    declaration
  • the actual name of the function is used whenever
    we want the function to invoke itself

7
sample program
  • calculate the nth Fibonacci number recursively

8
FUNCTION
  • PROGRAM Fibonacci
  • IMPLICIT NONE
  • INTEGER num, fib
  • WRITE(,) This program is to calculate the nth
    Fibonacci number.
  • WRITE(,) Please type in the value of n
    (nonnegative integer)
  • READ(,) num
  • IF (numlt0) THEN
  • WRITE(,) The number you input must be gt 0!
  • ELSE
  • WRITE(,) The result is , fib(num)
  • END IF
  • END PROGRAM Fibonacci

invoke the function
9
  • RECURSIVE FUNCTION fib ( n ) RESULT ( answer )
  • INTEGER, INTENT (IN) n
  • INTEGER answer
  • IF ( n gt 1 ) THEN
  • answer fib(n-1)fib(n-2)
  • ELSE IF (n1) THEN
  • answer 1
  • ELSE IF (n0) THEN
  • answer 0
  • END IF
  • END FUNCTION fib

invoke itself
10
SUBROUTINE
  • PROGRAM Fibonacci
  • IMPLICIT NONE
  • INTEGER num, answer
  • WRITE(,) This program is to calculate the nth
    Fibonacci number.
  • WRITE(,) Please type in the value of n
    (nonnegative integer)
  • READ(,) num
  • IF (numlt0) THEN
  • WRITE(,) The number you input must be gt 0!
  • ELSE
  • CALL fib(num, answer)
  • WRITE(,) The result is , answer
  • END IF
  • END PROGRAM Fibonacci

invoke the subroutine
11
  • RECURSIVE SUBROUTINE fib ( n, answer )
  • INTEGER, INTENT (IN) n
  • INTEGER, INTENT (OUT) answer
  • INTEGER answer1, answer2
  • IF ( n gt 1 ) THEN
  • CALL fib(n-1, answer1)
  • CALL fib(n-2, answer2)
  • answer answer1answer2
  • ELSE IF (n1) THEN
  • answer 1
  • ELSE IF (n0) THEN
  • answer 0
  • END IF
  • END SUBROUTINE fib

invoke itself
12
(No Transcript)
13
Towers of Hanoi
Goal Move disks from peg A to peg B. Rule May
only move one disk at a time, onto a bigger disk
or an empty peg.
1
2
3
A
B
C
source
dest
inter
14
Some initial steps
2
3
1
A
B
C
3
2
1
A
B
C
source
dest
inter
15
Breaking the goal into subgoals
1
3
2
A
B
C
1
3
2
A
B
C
source
dest
inter
16
Some final steps
3
2
1
A
B
C
2
3
1
A
B
C
source
dest
inter
17
Implementation of Towers of Hanoi
PROGRAM TOH !Moves n disks (ngt0) from peg source
to peg dest using peg inter as !intermediate. IMPL
ICIT NONE INTEGER n CHARACTER (len1)
A'A', B'B', C'C' WRITE(,) Please type in
the value of n READ(,) n IF (nlt0)
THEN WRITE(,) n must be gt 0! ELSE CALL
Hanoi (n, A, B, C) END IF END PROGRAM
18
  • RECURSIVE SUBROUTINE Hanoi (n, source, dest,
    inter)
  • IMPLICIT NONE
  • INTEGER n
  • CHARACTER (len1) source, dest, inter
  • IF (ngt0) THEN
  • CALL Hanoi (n-1, source, inter, dest)
  • WRITE(,) Move disk from peg, source, to
    peg, dest
  • CALL Hanoi (n-1, inter, dest, source)
  • END IF
  • END SUBROUTINE

19
characters in computers
  • How many characters are supported in computers?
  • ASCII character set
  • page 719-old textbook (871-new textbook),
    Appendix A
  • How to express each character?
  • each character is saved as a 8 bits sequence
    according to its index in the ASCII character set
  • for example
  • a is saved as 0110 0001 (97 in decimal)
  • A is saved as 0100 0001 (65 in decimal)
  • 5 is saved as 0011 1001 (53 in decimal)

20
character manipulations
  • a character string is an array of characters
  • for example
  • aAbB is a character string, which consists of 4
    characters continuously
  • aAbB is saved as 0110 0001 0100 0001 0110
    0010 0100 0010
  • We have lots of functions to handle characters
    and character strings.
  • ACHAR, IACHAR, LEN, LEN_TRIM, TRIM (150-old
    textbook, 159-new textbook)
  • More in Chapter 10 (429-old textbook, 457-new
    textbook)

21
  • TRIM(str1) returns str1 with trailing blanks
    removed

22
  • LEN(str1) returns length of str1
  • LEN_TRIM(str1) returns length of str1, excluding
    any trailing blanks

23
  • ACHAR(int1) returns the character corresponding
    to int1 in the ASCII collating sequence

24
  • IACHAR(char1) returns the integer corresponding
    to char1 in the ASCII collating sequence

25
  • concatenation operator // combines two or more
    strings or substrings into a single larger string

26
exercise 1
  • the combinatorial coefficient C(n, r) is defined
    recursively as follows
  • write a program to calculate the combinatorial
    coefficient, given n and r, by using a recursive
    function or a recursive subroutine

27
(No Transcript)
28
exercise 2
  • write a Fortran program
  • it works on two strings
  • the first string is Today is Tuesday.
  • the second string is Today is November 20,
    2007.
  • concatenates two strings to get a new string
    Today is Tuesday, November 20, 2007.
  • then changes all the uppercase letters to
    lowercase letters
  • prints out the original strings and the new
    strings
  • Hint refer to the example on page 151 of old
    textbook (160 new textbook)
Write a Comment
User Comments (0)
About PowerShow.com