Back

Solution: 2017 Fall Midterm - 15

Author: Michiel Smid

Question

Consider the recursive algorithm $\Hello$, which takes as input an integer $n \geq 0$: (see document for missing code) If you run algorithm $\Hello(5)$, how many times is the word "hello" printed?
(a)
14
(b)
16
(c)
13
(d)
15

Solution

We can draw a recursive tree to see how many times the word \enquote{hello} is printed.

image

Each time HELLO is called, \enquote{hello} is printed once. We can count the number of times HELLO is called

After counting, we see that \enquote{hello} is printed 15 times.