Recursion
Write the recursive function print_multiples(int max)
that prints the multiples of 7 between 7 and max
, in descending order. Assume that max is a multiple of 7.
The function call:
print_multiples(98);
Would produce the output:
98
91
84
77
70
63
56
49
42
35
28
21
14
7
Write the recursive function print_powers(int max)
that prints the powers of 2, in ascending order, that are less than or equal to max
. Assume that max
is a power of 2.
The function call:
print_powers(64);
Would produce the output:
1
2
4
8
16
32
64
The function call:
print_powers(1024);
Would produce the output:
1
2
4
8
16
32
64
128
256
512
1024
Write the recursive function prompt(int min, int max)
that repeatedly prompts the user to enter an integer until the user enters an integer in the range \([min, max]\).
The function call:
prompt(0, 100);
Might produce the output:
Enter an integer in the range [0, 100]: -1
Enter an integer in the range [0, 100]: 101
Enter an integer in the range [0, 100]: 0
The function call:
prompt(0, 10);
Might produce the output:
Enter an integer in the range [0, 10]: -1
Enter an integer in the range [0, 10]: 11
Enter an integer in the range [0, 10]: 5
What does your implementation of prompt do when min
is equal to max
?
How about when min
is greater than max
?
Write the recursive function print(int array[], int n)
that prints the first n
numbers in the array. Assume that
n
is at least 0 and less than the size of the array.
The function call:
Let a
be the array {8,12,13,4} of size 4.
print(a, 2);
Would produce the output:
8 12
print(a, 4);
Would produce the output:
8 12 13 4
print(a, 0);
Would output nothing.