Skip to main content

Section 24.13 Exercises

Checkpoint 24.13.1.

Write a recursive function void printPattern(string s). It will be passed a string of stars (e.g. "****") and should print out a pattern of stars that looks like this:
****
***
**
*
Hint.
What is the shortest pattern you need to print. That will be your base case.

Checkpoint 24.13.2.

Write a recursive function void printPattern(string s). It will be passed a string of stars (e.g. "****") and should print out a pattern of stars that looks like this:
*
**
***
****
Hint 1.
What is the shortest pattern you need to print. That will be your base case.
Hint 2.
The general case should do its work after the recursive call.

Checkpoint 24.13.3.

Write a recursive function void printPattern(string s). It will be passed a string of stars (e.g. "****") and should print out a pattern of stars that looks like this:
****
***
**
*
**
***
****
Hint 1.
What is the shortest pattern you need to print. That will be your base case.
Hint 2.
The general case will need to do work before and after the recursive call.

Checkpoint 24.13.4.

Implement the recursive function: string repeat(string s, int times)
The returned value should be the string s repeated times times. You don’t have to worry about negative values of times. Note that the function header is there, you just need to fill in the body.
Hint 1.
You do need to handle 0 repetitions. What string should that produce? That is your base case.
Hint 2.
Say you were told to repeat the string "hello" 5 times and knew you could ask me what the value of repeating "hello" 4 times was. How could you get your answer?

Checkpoint 24.13.5.

Implement a recursive function to turn an integer into a string of binary digits (1s and 0s) string makeBinary(int n)
Use the divide by 2 method to convert decimal to binary (you can refer to Welcome to CS for how the algorithm works).
The logic for converting 13 to binary (1101):
Number /2 Quotient /2 Remainder Result Comment
13 6 1 "1"
Add 1 to answer, continue working with 6
6 3 0 "01"
Add 0 to answer, continue working with 3
3 1 1 "101"
Add 1 to answer, continue working with 1
1 0 1 "1101"
Add 1 to answer, continue working with 0
0 0 0 "1101" Once we reach 0, stop.

Checkpoint 24.13.6.

Write a function double investment(double money, double interestRate, int years) that returns the final value of an amount of money invested at a given interest rate for a given number of years.
years will never be negative, but it may be 0.
The interest rate will be given as a percentage (e.g., 5.0 for 5%). Compute simple interest for each year. E.g. Starting with 10000 and an interest rate of 5%, the interest earned for the first year will be 10000 * 0.05 = 500.
Hint 1.
The smallest value you need to handle is your base case.
Hint 2.
Each recursive case represents one more year of your money having been invested.

Checkpoint 24.13.7.

Write a function int investmentLength(double money, double goal, double interestRate) that returns the number of years it will take to reach the given investment goal if you start with the given amount of money and invest at the specified interest rate.
The interest rate will be given as a percentage (e.g., 5.0 for 5%). Compute simple interest for each year. E.g. Starting with 10000 and an interest rate of 5%, the interest earned for the first year will be 10000 * 0.05 = 500.
Hint 1.
How would you know it will take 0 years to reach the goal? That will be your base case.
Hint 2.
If you leave the money invested until next year, what would the new amount of money be? Would the goal or interest rate change for the recursive call?
Hint 3.
Each recursive case represents one more year of your money having been invested. It should return a value one larger than the result of the recursive call with updated parameters.

Checkpoint 24.13.8.

Implement the function: double series(int steps)
This function should return the sum of the first steps number of terms from the series 1 + 1/2 + 1/4 + 1/8 + 1/16....
For example:
You can add another parameter. Although you do not have to, it may make the logic easier. If you want to do so, make sure it has a default value or you have a non-recursive version of the function to kick start things.
Hint.
Add a parameter that stores the denominator to use for the current term. 1 is the same as 1/1.

Checkpoint 24.13.9.

Implement the function int letterCount(const string& s, char a). It should return the number of times char a appears in s.
If you want to avoid making substrings of s, you can create a recursive helper that the provided letterCount calls.

Checkpoint 24.13.10.

Write a function int numLegs(const vector<char>& animals, int curPosition = 0). The vector will consist only of the chars ’c’ and ’d’ representing "cow" or "duck". The function should calculate the total number of legs those animals have (assume no mutations or industrial accidents... a cow has 4 and a duck has 2).
Use the changing index strategy.
Hint.
curPositioncurPosition.size()

Checkpoint 24.13.11.

Implement the function int getMax(const vector<int>&, int curIndex = 0). It should return the largest int in the vector.
Hint.
.size() - 1
You have attempted of activities on this page.