Solutions for Tutorial #3

advertisement
Kingdom of Saudi Arabia
‫اململكة العربية السعودية‬
Ministry of Higher Education
‫وزارة التعليم العايل‬
Salman Bin Abdulaziz University
‫جامعة سلمان بن عبدالعزيز‬
College of Computer Engineering and sciences
‫كلية هندسة وعلوم احلاسب‬
‫قسم علوم احلاسب‬
Computer Science Department
CS2320: Data Structures & Algorithms
Tutorial #03: Recursion + Answers
Part I: Choose the correct answer(s) and write the answers down in the table provided above:
1. A method that calls itself is a __________________ method.
a) Invalid
b) Static
c) Final
d) Recursive
Explanation: A recursive method is a method that calls itself.
2. ____________________ recursion occurs when a method calls itself, while _________________ recursion when a method
calls another method that then calls the original method.
a) upward, downward
b) downward, upward
c) direct, indirect
d) indirect, direct
Explanation: Direct recursion occurs when a method calls itself, while indirect recursion occurs when a method calls
another method that then calls the original method.
3. __________________ recursion results from the lack of a base case.
a) Indirect
b) Direct
c) Infinite
d) Spiral
Explanation: Infinite recursion results from the lack of a base case, which causes the recursive path to be followed forever.
4. The _______________________ puzzle is a favorite of computer scientists because of its elegant recursive solution.
a) Tower of Hanoi.
b) Sudoku.
c) Tetris.
d) Tic-Tac-Toe.
Explanation: The Tower of Hanoi puzzle has an elegant recursive solution, and has therefore become a favorite of
computer scientists.
5. In the Towers of Hanoi puzzle, there are ________________ pegs.
a) 2.
b) 3.
c) 4.
d) 5.
Explanation: In the Towers of Hanoi puzzle, there are always exactly three pegs.
1
6. In the Towers of Hanoi puzzle there are __________________ disks of different diameters.
a) 3.
b) 4.
c) 5.
d) The Towers of Hanoi puzzle can include any number of disks of different diameters.
Explanation: There can be any number of disks in the Towers of Hanoi puzzle.
7. Which of the following statements is true?
a) Recursion should be used in every program.
b) Recursion should be avoided all the time.
c) Solutions that are easily expressed recursively should be programmed recursively.
d) Solutions that are easily expressed recursively should be programmed iteratively.
Explanation: Recursion should be used when the solution is easily expressed recursively.
8. Which of the following will usually result from infinite recursion?
a) The program will hang as though there is an infinite loop.
b) The program will not compile.
c) The program will run out of memory.
d) None of the above.
Explanation: Infinite recursion will cause a program to run out of memory.
9. In the Towers of Hanoi puzzle __________________________________ .
a) It is legal to move a disk onto a peg that contains smaller disks.
b) It is legal to sit a disk aside, not on any peg.
c) It is illegal to move a disk onto a peg that contains only larger disks.
d) None of the above are true
Explanation: Neither choice a, choice b or choice c make the statement true.
10. Almost all recursive programs ____________________________________ .
a) Are more difficult to read than iterative programs.
b) Can be implemented iteratively.
c) Are easier to read than iterative programs.
d) Are shorter than iterative programs.
Explanation: Most recursive programs can be implemented iteratively, although a recursive solution may be easier to code.
11. The recursive solution of the Towers of Hanoi problem has _______________ complexity.
a) Exponential.
b) Polynomial.
c) Logarithmic
d) None of the above
Explanation: The recursive solution of the Towers of Hanoi problem has exponential complexity, which means that it is
significantly inefficient.
12. Which of the following is a proper recursive definition of x raised to the y power?
a) xy = x*xy-1
b) xy = x*xy-2
c) xy = x*xy-1 for y > 1; xy = x for y = 1
d) xy = x*xy-1 for all x
Explanation: Choice c is correct because it has a properly defined base case.
2
13. There is(are) ____________ base case(s) in the recursive solution to finding a path through a maze.
a) 0
b) 1
c) 2
d) 3
Explanation: There are two base cases in the recursive solution to this problem. The two base cases are: (1) a path has
been found, and (2) there is no way to move forward via the current path.
14. A solution with exponential complexity is ____________________ .
a) Efficient.
b) Inefficient.
c) Easy to implement.
d) Difficult to implement.
Explanation: A solution with exponential complexity is very inefficient.
15. In a recursive solution, _______________ is(are) always necessary.
a) Short, efficient coe.
b) Several variables.
c) Numerous lines of code.
d) Base case.
Explanation: A base case is always necessary in a recursive solution to avoid infinite recursion.
16. A recursive function defining the factorial for n > 1 is, the function facty(n):
a)
b)
c)
d)
facty(n) = n * facty(n-1)
facty(n) = facty(n) = n * facty(1-n)
facty(n) =n-1 * facty( n-2)
None of the above.
Explanation: Choice a is correct because it has properly defined recursive case.
17. A recursive function defining the Fibonacci number n  2 , Fabio(n) is
a)
b)
c)
d)
Fabio(n) =Fabio(n-1) + Fabio(1-n)
Fabio(n) =Fabio(n-1) + Fabio(n-2)
Fabio(n) =Fabio(n-1) + Fabio(Fabio n-2))
None of the above.
Explanation: Choice b is correct because it has properly defined recursive case.
3
PART II: Coding
Iterative (non-recursive)
Recursive
1) Write an iterative method to compute the power of x n for
non-negative n.
public static int iterativePower(int x, int n)
{
int result = 1;
if(n < 0)
{
Cout <<"Illegal argument to
power";
Break ;
}
2) Write a recursive method to compute the power of x n for
non-negative n.
public static int power(int x, int n)
{
if(n < 0)
{
Cout <<"Illegal argument to
power";
Break ;
}
if(n > 0)
return ( x * power(x, n-1) );
//recursive case
else
return(1);
//base case
for(int i = 1; i <= n; ++i)
result = result * x;
return result;
}
}
3) Write an iterative method to compute the factorial of a
number.
public static int iterativeFactorial(int n)
{
if(n < 0)
{
Cout << ”Sorry negative numbers
not allowed.”;
Break;
}
int result = 1;
for(int i = 1; i <= n; ++i)
result = result * i;
4) Write a recursive method to compute the factorial of a
number.
public static int factorial(int n)
{
if(n < 0)
{
Cout <<“Sorry negative numbers
not allowed.” ;
System.exit(0);
}
if(n == 1)
//base case
return(1);
else
return (n * factorial(n-1));
//recursive case
return result;
}
}
4
Download