Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Write a recursive function that takes in a 2 character string and a number o

ID: 665095 • Letter: C

Question

C++

Write a recursive function that takes in a 2 character string and a number of times to repeat
the pattern. The function should return a string that has the rst character repeated the
speci ed number of times followed by the second character repeated the speci ed number of
times. The function declaration should be as follows:
std::string createPattern(string pattern, int numRepeats);
Hint: You can index into a string using [] or substring. You can concatenate strings using
+.
For example, if you call createPattern(()", 4), it should return (((())))".

Explanation / Answer

function repeatRecursive(string:String, numTimes:uint):String { if(numTimes == 0) return ""; if(numTimes & 1) return string + repeatRecursive(string, numTimes - 1); var tmp:String = repeatRecursive(string, numTimes/2); return tmp + tmp; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote