CENG113 Problem Sets 9 - Recursion
1. Write a recursive method for each of the following problems.
a) Find the product of all integers from X to Y, inclusively.
b) Find the sum of a specific row of a given matrix.
c) Find the position of the minimum number in a one-dimensional array.
d) Find the product of only the elements in even numbered positions of a one-dimensional array.
e) Find how many multiples of N exist between X and Y. For instance, if X is 5, Y = 10, N = 2, the
result should be 3, because there are 3 multiples of 2 between 5 and 10: 6, 8, and 10.
f) Find the sum of the multiples of N that exist between X and Y. For instance, if X is 5, Y = 10, N =
2, the result should be 24, because the multiples of 2 between 5 and 10 are 6, 8, and 10.
g) Given a positive integer, display its digits in the reverse order. For instance, if the given integer is
1234, the output should be 4321.
h) Given a string, display its characters in the reverse order. For instance, if the given string is
"abcd", the output should be "dcba".
i) Find the number of occurrences of a certain character within a string. For instance, if the string is
"daddy", and the character is 'd', the result should be 3.
j) Find the position of the last non-blank character of a string. For instance, if the string is "ALI
AKIN ", the result should be 7.
k) Find the position of the last occurrence of a double number in a one-dimensional array, using
linear search algorithm, recursively.
l) Find the position of the last occurrence of a double number in an ascendingly sorted one-
dimensional array, using linear search algorithm, recursively.
m) Sort a list of double numbers (stored in a one-dimensional array), using selection sort algorithm,
recursively.
n) Sort a list of double numbers (stored in a one-dimensional array), using bubble sort algorithm,
recursively.
o) Find the greatest common divisor of two positive integers. The greatest common divisor of two
positive integers M and N, GCD (M, N), is the largest positive integer that divides both M and N.
Thus, for instance, GCD (100, 5) == 5, GCD (100, 33) = 1, GCD (102, 30) = 6. This can be found
by using the division algorithm, as follows:
102 = 30 * 3 + 12
30 = 12 * 2 + 6
12 = 6 * 2 + 0
Notice that:
GCD (102, 30) = GCD (30, 12)
= GCD (12, 6)
= 6
In each case, the remainder is used in the next step. The process terminates when a remainder of
0 is obtained.
Recursion – Page 1
2. Write recursive methods that display each of the following outputs when they are called
with the string on the first line.
a) THE EXAM b) THE EXAM IS VERY EASY
HE EXAM HE EXAM IS VERY EAS
E EXAM E EXAM IS VERY EA
EXAM EXAM IS VERY E
EXAM EXAM IS VERY
XAM XAM IS VERY
AM AM IS VER
M M IS VE
IS V
IS
S
Recursion – Page 2