logo

UTK Notes


Clicker Questions - 22-Recursion

Clicker Questions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void a(const string &a, int index)
{
    if (index == a.size()) return;

    if (s[index] == 'A') {
        cout << '(';
        a(s, index+1);
        cout << ')';

    } else {
        cout << "()";
        a(s, index+1);
    }
}

int main()
{
    string s;
    
    cin >> s;
    a(s, 0);
    cout << endl;
    return 0;
}

Suppose that the program above is complied into the executable a.out. Please answer with the output of the following:

Question 1:

1
echo A | ./a.out
Answer
UNIX> echo A | ./a.out
()

Question 2:

1
echo AA | ./a.out
Answer
UNIX> echo AA | ./a.out
(())

Question 3:

1
echo BA | ./a.out
Answer
UNIX> echo AA | ./a.out
()()

Question 4:

1
echo BBBAA | ./a.out
Answer
UNIX> echo AA | ./a.out
()()()(())

Question 5:

1
echo AAABB | ./a.out
Answer
UNIX> echo AA | ./a.out
(((()())))

Class Stats, PDF Download