go to previous page   go to home page   go to next page hear noise highlighting

Answer:

From a static viewpoint, what is wrong?

The base case Triangle(1) = 1 was left out of the Java code.

From a dynamic viewpoint, what is wrong?

The method always calls for another activation regardless of the value of the parameter, so the chain of activation keeps growing until system resources run out.

Computational Advantages of Iteration and Recursion

Recursion is useful because sometimes a problem is naturally recursive. Then all you need to do is match it with Java code. But recursion does not add any fundamental power to Java.

Any method that uses recursion can be written using iteration.
Any method that uses iteration can be written using recursion.

A computer programming language needs to include only one of iteration or recursion to be as powerful as any other programming language. Some languages (such as early FORTRAN) have only iteration. Other languages (such as early LISP) have only recursion. Most modern languages have both, because both are useful.


QUESTION 20:

Can your write an iterative version of Triangle?


go to previous page   go to home page   go to next page