Question
Consider the following statement: construct a recursive function to display the digits of any positive integer in reverse order on the console display. This is
Consider the following statement: "construct a recursive function to display the digits of any positive
integer in reverse order on the console display". This is a void function that would take a single argument:
void reverseNum(int n);
and, given an integer value such as:
reverseNum(5786);
would output the following to the console display:
6875
a.
Start by identifying base case(s) that is trivial to solve. For this problem, perhaps the easiest
integer to "reverse" would be any value with a single digit. In this case, you would simply display the
input value and be done. Write the C++ statement or statements to implement the base case and
return:
b. Next, determine if there is some way the original problem can be represented as the base case
combined
with a recursive call to the function using a reduced version of the original input. For this
particular problem, note that if the argument contains more than one digit, reversing it entails
displaying the rightmost digit first, followed by the rest of the digits in reverse order. The "
rest of the
digits in reverse order
" is simply a reduced version of the original problem! First, write the C++
statement or statements that will display the "rightmost" digit:
c. Now, write the
recursive
reduction step that calls the function for the "
rest of the digits
:
1
d. Combining these two steps forms the complete reduction case. If this is the only reduction step, it
is simply performed for any but the base case(s). Structure the steps above as an
else
clause (youll
add it to an
if
clause in the next step) that contains the entire reduction step:
e. Before you complete the function, take a minute to ensure that (i) the base case represents a correct
solution, and (ii) the reduction step is guaranteed to get you "closer" to the base case. Now combine
the base and reduction steps and write the complete function definition:
2) Consider a
recursive
function to find the maximum value in an array of integers. The function declaration
is:
int maxValue( int vals[], int size, int start );
For this function, we need to know the size of the array
and
the starting index of the array (because both will
change when a recursive call is made). You may assume that there is at least one value in the array:
a. Describe the base case: [Hint: in what size array would it be easiest to find the maximum value?]
b. Describe the reduction step: [Hint: describe the original problem using the base case in
combination with some reduced form of the problem.]
c. Describe why the reduction step will (i) solve the problem and (ii) move closer to the base case:
d. Write the complete function definition for maxValue
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started