CS341 Lab 7

Telechargé par azzaguidara
CS341
1
CS341 Lab7
Objectives
• Understand the concept of recursion.
• Implement examples of recursion.
Exercise 1
Write a recursive method called exponent(x,y) to perform exponentiation return xy , assuming y
>= 0.
Example: exponent(10,3) → will produce an output of 1000
Exercise 2
The problem of counting the digits in a positive integer or summing those digits can be solved
recursively. For example, to count the number of digits think as follows:
If the integer is less than 10 there is only one digit (the base case). Otherwise, the number of digits
is 1 (for the units digit) plus the number of digits in the rest of the integer (what's left after the units
digit is taken off). For example, the number of digits in 3278 is 1 + the number of digits in 327.
Example: SumofDigits(732) will produce 12.
Exercise 3
We are in the presence of multiple recursions when the activation of a method can causes more
than one recursive activation of the same method.
Example: Recursive method for computing the n-th Fibonacci number.
F(n) . . . number of individuals at the n-th generation.
F(n) =
0, if n = 0;
1, if n = 1;
CS341
2
and F(n − 2) + F(n − 1), if n > 1
F(0), F(1), F(2), . . . is called the sequence of Fibonacci numbers, and it starts with: 0, 1, 1, 2, 3, 5,
8, 13, 21, . . .
Implement a recursive method that takes a positive integer n as parameter and returns the n-th
Fibonacci number.
Exercise 4:
Rewrite the previous program using loops and compare their running time.
Exercise 5:
Provide an implementation of the method that calculates the Ackermann function A(m, n), which
is defined as follows:
A(m, n) =
n + 1, if m = 0 (base case)
A(m − 1, 1), if n = 0 (recursive case)
A(m − 1, A(m, n − 1)), otherwise (recursive case)
Note that the Ackermann function grows very fast (it is a non-elementary function): A(x, x) grows
faster than any tower of exponentials 22 ···2^x .
Exercise 6
A palindrome is a string that is the same forward and backward. It is possible to define a
palindrome recursively as follows:
A string containing fewer than 2 letters is always a palindrome.
A string containing 2 or more letters is a palindrome if its first and last letters are the same,
and the rest of the string (without the first and last letters) is also a palindrome.
Write a program that prompts for and reads in a string, then prints a message saying whether it is
a palindrome. Your main method should read the string and call a recursive (static) method
palindrome that takes a string and returns true if the string is a palindrome, false otherwise.
CS341
3
Recall that for a string s in Java,
s.length() returns the number of charaters in s
s.charAt(i) returns the ith character of s, 0-based
s.substring(i,j) returns the substring that starts with the ith character of s and ends with the
j–1st character of s (not the jth), both 0-based.
Exercise 7
Write a program called Permutations that takes an integer command-line argument n and prints
all n! permutations of the n letters starting at a (assume that n is no greater than 26).
A permutation of n elements is one of the n! possible orderings of the elements. As an example,
when n = 3 you should get the following output (but do not worry about the order in which you
enumerate them): abc, acb, bac, bca, cab, cba
1 / 3 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !