GATE CS 2021 Set 2 | Question 59

Programming & Data Structures › Functions · NAT · 2 marks · Answer: 60

  1. Question 59Programming & Data Structuresnat2 marks
    Consider the following ANSI C program.
    #include <stdio.h>
    int foo(int x, int y, int q) {
        if ((x <= 0) && (y <= 0)) return q;
        if (x <= 0) return foo(x, y - q, q);
        if (y <= 0) return foo(x - q, y, q);
        return foo(x, y - q, q) + foo(x - q, y, q);
    }
    int main() {
        int r = foo(15, 15, 10);
        printf("%d", r);
        return 0;
    }
    The output of the program upon execution is ______.

    Answer: 60

    Solution

    The recursion counts the number of monotone lattice paths (steps of size decreasing or ) from down to the base region, each contributing . Evaluating the recursion gives the output 60.

More GATE PYQ on Functions

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2021 Set 2 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →