• No results found

Recursive Factorial Function Call Tree

In document Fortran 95 (Page 172-176)

In order to better understand recursion, a recursion tree can help show how the recursive calls interact.

When the initial call to factorial function occurs from main, the main will start into the fact() function (shown as step 1). Since the argument of 5 is not a base case, the fact() function must call fact() again with the argument of n-1 or 4 in this example (step 2). And, again, since 4 is not the base case, the fact() function must call fact() again with the argument of n-1 or 3 in this example (step 3).

This process continues until the argument passed into the fact() function meets the base case which is when the arguments is equal to 1 (shown as step 5). When this occurs, only then is a return value provided to the previous call (step 6). This return argument is then used to calculate the previous multiplication which is 2 times 1 which will return a value to the previous call (as shown in step 7).

This process will continue (steps 8, 9, and 10) until the main has a final answer.

Since the code being executed is the same, each instance of the fact() function is different from any other instance only in the arguments and any local values (none in this example).

Illustration 3: Factorial Recursion Tree fact:

Chapter 18 ◄ Recursion It should also be noted that the height of the recursion tree is directly associated with the amount of memory used by the recursive function. For problems where the recursion tree is very large, this can have a negative impact on overall performance of a recursive routine.

18.6 Exercises

Below are some quiz questions and project suggestions based on this chapter.

18.6.1 Quiz Questions

Below are some quiz questions.

1) What are the two requirements for a recursive definition?

2) In recursion, the case for which a solution is obtained directly is called what?

3) What keyword is required for a recursive subroutine?

4) What two keywords are required for a recursive function?

5) What special requirements are required of the calling routine in order to call a recursive subroutine or recursive function?

6) For a recursive routine, what would happen if the routine does not stop recursing?

7) Create a recursion tree for the recursive Fibonnaci function (as described in the following suggested projects section) with the input of 13. Note, the recursive Fibonnaci function requires two recursive calls for for the non-base case step.

18.6.2 Suggested Projects

Below are some suggested projects.

1) Type in the print binary main program and recursive subroutine. Test on several data sets and verify that the program produces the correct results.

2) Type in the factorial main program and recursive funciton. Test on several data sets and verify that the program produces the correct results.

3) The recursive definition of Fibonnaci function is as follows:

fib(n) =

{

11fib( n−1)+ fib(n−2) if n=0if n=1if n≥ 2

Create a main program to read the n value from the user and ensure it is between 1 and 40.

Develop recursive function, fib, to recursively compute the Fibonnaci number based on the provided definition. Note, the recursive Fibonnaci function requires two recursive calls for for the non-base case step.

Chapter 18 ► Recursion

4) Develop a recursive subroutine to recursively print a star tree. Based on an initial value, n, the star tree should be displayed. For example, for an n value of 5, the program should output something similar to the following:

Recursive Subroutine Program Enter Number of Stars: 5 Star Tree:

* * * * *

* * * *

* * *

* *

*

Create a main program to read the n value from the user and ensure it is between 1 and 50.

Develop recursive subroutine, printStars(), to recursively print the start tree as shown. The subroutine should print one line per call. For successive recursive calls, the n value passed as an argument should be decremented. The based case would be one (1) star.

5) Write a program using a recursive function to determine the number of possible paths through a two-dimensional grid. The only allowed moves are one step to the right or one step down. For example, given a grid as follows:

0 1 2

0 start

1 2

3 end

Moving from the starting location, (0,0) in this example, going to the end location, (3,2) in this example, can be performed in 10 different ways. Two, of the ten, different ways are shown in the example above. The function must be recursive.

Create a main program to read the initial grid coordinates and ensure that they are valid (positive values) and that the end coordinates are greater than the start coordinates. Create a recursive function, countPaths(), to determine the number of possible paths through a two-dimensional grid. The function will accept a start coordinate (row,col) and a final end coordinate (row,col).

Chapter 18 ◄ Recursion 6) The Tower of Hanoi is a mathematical puzzle that consists of three pegs, and a number of disks

of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape.

The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:

Only one disk may be moved at a time.

Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.

No disk may be placed on top of a smaller disk.

The following is a recursive definition for the problem:

hanoi( n , from , to , by ) =

{

write(move the disc from from to to) if n=1 hanoi ( n−1, from , by , to) if n>1 hanoi (1, from , to , by)

hanoi (n−1, by , to , from)

Create a main program to read and validate the number of disks, n, from the user and ensure it is between 1 and 10. Develop recursive function, hanoi, to recursively compute a solution to the Tower of Hanoi problem.

19 Character String / Numeric Conversions

Characters string values, such as “123” can not be used to perform numeric operations such as addition or multiplication. As such, for more complex programs, there is sometimes the need to convert

between a character string representing a numeric value and an actual real or integer number.

These conversions can be performed using what is referred to as an internal read or an internal write.

Basically, the read or write functions and associated format statements can be used to perform basic conversions. Instead of reading from an open file, the read and write operations can read and write directly from and to variables. The specified format provides guidance for the conversion result.

Based on the input, a conversion may not be possible. For example, the character string “3.14” can be converted into the real value of 3.14. However, the character string “3.1z4” could not be converted since the 'z' is not a legal numeric value.

If a conversion is not possible, an error would be generated. If not handled, such an error would crash the program. In order to address and handle any potential errors, the iostat parameter for the read/write operation is used as previously described in the file operations chapter.

In document Fortran 95 (Page 172-176)