Java Reference
In-Depth Information
C(n, r) denotes the number of ways r different things can be chosen from a
set of n items. Then C(n, r) is given by the following formula:
n!
r! ð n r Þ !
C ð n ; r Þ¼
where the exclamation point denotes the factorial function. Moreover, C(n,
0) ¼ C(n, n) ¼ 1. It is also known that C(n, r) ¼ C(n -1,r -1)+C(n -1,r).
a. Write a recursive algorithm to determine C(n, r). Identify the base
case(s) and the general case(s).
b. Using your recursive algorithm, determine C(5, 3) and C(9, 4).
PROGRAMMING EXERCISES
1. Write a recursive method that takes as a parameter a nonnegative integer and
generates the following pattern of stars. If the nonnegative integer is 4, then
the pattern generated is:
****
***
**
*
*
**
***
****
Also, write a program that prompts the user to enter the number of lines in
the pattern and uses the recursive method to generate the pattern. For
example, specifying the number of lines to be 4 generates the preceding
pattern.
2. Write a recursive method to generate the following pattern of stars:
*
* *
* * *
* * * *
* * *
* *
*
Also, write a program that prompts the user to enter the number of lines in
the pattern and uses the recursive method to generate the pattern. For
example, specifying the number of lines to be 4 generates the preceding
pattern.
3. Write a recursive method, vowels , that returns the number of vowels in a
string. Also, write a program to test your method.
1
3
 
Search WWH ::




Custom Search