Problems Requiring the Analysis of Recursive Functions The following problems will give you another excellent opportunity to hone your recursive skills. These problems do not require to you write recursive functions. Instead they require you to study some functions (all of which happen to be recursive), decide what each function does, and then use that knowledge to deduce what output or return value the function produces for the given input. At least this is the best approach to take. If you cannot decide what the function does by studying it, you should still be able to trace the function for the given input to find the necessary output or return value, but doing so should be regarded as a last-ditch method of attack. ----- Problem ---------------------------------------------------------- void DoIt(/* in */ int n) { if (n < 10) cout << n << n; else { cout << n % 10; DoIt(n / 10); cout << n % 10; } } What is this function doing? ___________________________________________ ________________________________________________________________________ Give the output from each of the following function calls: a) DoIt(34567) --> ____________________ b) DoIt(300) --> ____________________ c) DoIt(2442) --> ____________________ ----- Problem ---------------------------------------------------------- void DoIt(/* in */ int n) { if (n < 10) { if (n % 2 == 1) cout << n; } else { int t; t = n % 10; DoIt(n / 10); if (t % 2 == 1) cout << t; } } What is this function doing? ___________________________________________ ________________________________________________________________________ Give the output from each of the following function calls: a) DoIt(6798) --> ____________________ b) DoIt(73591) --> ____________________ c) DoIt(824163) --> ____________________ ----- Problem ---------------------------------------------------------- bool f(/* in */ int n) { if (n < 10) return true; else if (n < 100) return (n / 10) == (n % 10); else { int a; int c; int b; int d = 1; while (n / d >= 10) d *= 10; a = n / d; b = (n % d) % 10; c = (n % d) / 10; return (a == b) && f(c); } } What is this function computing? _______________________________________ ________________________________________________________________________ Give the value of each of the following: a) f(3629263) --> ____________ b) f(6546) --> ____________ c) f(6) --> ____________ d) f(12324) --> ____________ e) f(123454321) --> ____________ ----- Problem ---------------------------------------------------------- int f(/* in */ int n) { if (n < 100) return 10 * (n % 10) + (n / 10); else return f(n / 100) * 100 + f(n % 100); } What is this function computing? _______________________________________ ________________________________________________________________________ Give the value of each of the following: a) f(362974) --> ____________ b) f(358) --> ____________ c) f(6) --> ____________ ----- Problem ---------------------------------------------------------- int f(/* in */ int n) { if (n < 10) { if (n % 2 == 0) return 1; else return 0; } else { if ((n % 10) % 2 == 0) return 1 + f(n / 10); else return f(n / 10); } } What is this function computing? _______________________________________ ________________________________________________________________________ Give the value of each of the following: a) f(654321) --> ____________ b) f(42622) --> ____________ c) f(7319531) --> ____________ ----- Problem ---------------------------------------------------------- void Doit(/* in */ int n) { if (n < 10) cout << n << n; else { int a; int b; int c = 1; while (n / c >= 10) c *= 10; a = n / c; b = n % c; cout << a; Doit(b); cout << a; } } What is this function doing? ___________________________________________ ________________________________________________________________________ Give the output from each of the following function calls: a) DoIt(72549) --> ____________________ b) DoIt(60) --> ____________________ c) DoIt(2563) --> ____________________ ----- Problem ---------------------------------------------------------- void DoIt() { char ch; cin.get(ch); if (ch >= 'A' && ch <= 'Z') cout << ch; if (cin.peek() != '\n') DoIt(); } What is this function doing? ___________________________________________ ________________________________________________________________________ Give the output when the input is each of the following: a) To be or NOT to be --> ______________________________________________ b) The House of the Rising Sun --> _____________________________________ c) A, B and C are the 1st 3 and X, Y and Z the last 3 --> ______________ ----- Problem ---------------------------------------------------------- void DoIt(/* in */ int n) { if (n < 10) cout << n * n; else { cout << (n % 10) * (n % 10); DoIt(n / 10); } } What is this function doing? ___________________________________________ ________________________________________________________________________ Give the output from each of the following function calls: a) DoIt(432) --> ____________________ b) DoIt(5348) --> ____________________ c) DoIt(70) --> ____________________ ----- Problem ---------------------------------------------------------- int f(/* in */ int n) { if (n < 3) return n; else { return f(n / 3) * 10 + (n % 3); } } What is this function computing? _______________________________________ ________________________________________________________________________ Give the value of each of the following: a) f(37) --> ____________ b) f(101) --> ____________ c) f(14) --> ____________