Lab 08
Lab 08
In this lab, you will get practice designing, implementing, and testing recursive solutions to various problems.
All computation MUST be done recursively; you should not write any kind of loop in the functions you
implement, and you should not use any libraries other than those already included in the starter code.
After entering your cs11 directory, download the starter code by entering the following command:
pull-code11 lab08
Learning Objectives
Introduction
Your goal is to design and implement a recursive function to solve the first two problems below; the third
problem is provided for those who want some additional challenge! You will implement these functions in
the provided recursion.cpp file, which contains starter code that will prompt the user to select a function
to run, get any appropriate arguments from the user, and pass them to the appropriate function. You’ll
solve the problems in this lab by designing, writing, and testing these functions.
For each of these problems, you should first design a recursive solution with a partner on paper. For each
problem, answer these questions to guide your designs:
1. You’ll be solving this problem with a function. What should be the inputs and output of that function?
2. What are the base cases for this problem? What are the simplest inputs the function could take, i.e,
on what inputs will the function barely have to do anything to produce the output?
3. Given an input corresponding to a base case, what should be the output?
4. What are the recursive cases for this problem? Given an input that doesn’t match a base case, how
can you get it closer to being a base case? If the problem were solved on that “simpler” input, what
else would have to be done with that result to solve the problem on the more complex input?
5. Given an input corresponding to a recursive case, what should be the output?
If you run into problems in your code and ask a TA for help, but haven’t designed a recursive solution on
paper or tested it manually, the TA will not help you. Feel free to ask the TAs for help if you get stuck
designing or testing your solution on paper!
We have provided a demo program, recursion demo, that you can run to see what each function should be
doing (in terms of input and output). If you would like to diff against the demo, feel free to create some
input files!
1
Problem 1: Powerful
Write a recursive function called power that computes the result of n raised to the power of k. In other
words, given integer arguments n and k, your function should return the value nk . You may assume that k
is a non-negative integer.
This problem can be solved recursively with one base case and one recursive case. After coming up with the
base case, figure out the recursive case by considering how solving a simpler version of this problem could
let you solve the version presented here. Look back to your notes or the lecture slides if you need some
inspiration.
Problem 2: sdrawkcaB
Write a recursive function called print reverse that prints out a string backwards (note that this void
function doesn’t actually “return” anything). For example, if given the string argument “Richard”, then the
function should print “drahciR”. You may assume that any string passed in has a length of 1 or more. Just
like with the previous problem, the simplest solution to this problem has one base case and one recursive
case.
You are encouraged (but not required) to use the length() function and the substr() function from the
C++ string library to help solve this problem. Given a string s and two integers position and length,
the expression s.substr(position, length) returns a new string initialized to the portion of s that starts
at index position and spans length characters (or until the end of the string, whichever comes first).
Write a recursive function called subseq that takes in two string arguments called s and t, and returns true
if string s is a subsequence of string t. “toga” is a subsequence of “strongbad” since the characters ‘t’, ‘o’,
‘g’ and ‘a’ all appear in the latter in that order. Note that these letters don’t need to appear contiguously.
“goat” is not a subsequence of “strongbad” – all requisite letters are there, but they don’t appear in a
consistent order. Which of “songa” and “ratdog” are subsequences of “strongbad”?
This problem is MUCH harder than the previous two (don’t worry if you don’t complete it in lab) both
because there are two arguments to worry about in the different cases of your recursive design, and because
you’ll need two base cases and two recursive cases to completely solve this problem. Focus on the base
cases first with your partner and convince yourselves that there aren’t any other simple situations where you
instantly know whether s is a subsequence of t.
Then move onto the recursive cases, which correspond to two situations: either the first letter of s matches
the first letter of t, or these first letters don’t match. What are the subproblems to solve in these two cases?
Submitting
When you are done or, if you aren’t finished but your lab session is ending, submit recursion.cpp using
the following command:
submit11-lab08
*Much thanks to Tom Wexler and Alexa Sharp for the description used in Problem 3.