Question 4: show the output (recursion) [6]


Show the precise output produced by the program below.

#include <cstdio>

void f1(int r, int c);
void f2(int c);

int main()
{
   f1(3,4);
}

void f1(int row, int col)
{
   if (row > 0) {
      f1(row - 1, col - 1);
      f2(col);
   }
}

void f2(int c)
{
   if (c > 0) {
      printf("%d", c);
      f2(c - 1);
   } else {
      printf("\n");
   }
}

Sample solution:
21
321
4321

Explanation
f1(3,4)
    calls f1(2,3)
             calls f1(1,2)
                      calls f1(0,1)
                               which simply ends and returns
                      calls f2(2)
                               which prints "21\n"
                      ends and returns
             calls f2(3)
                      which prints "321\n"
             ends and returns
    calls f2(4)
             which prints "4321\n"
    returns to main