Please help Consider the following pseudocode assuming neste
Please help!
Consider the following pseudo-code, assuming nested subroutines and static scope: main() {int g; int x; function B(int a) {x = a + 5; R(1);} function A(int n) {g:= n;} function R(int m) {print x; x = x/2; if (x > 1) R(m + 1); else A (m);}//body of main B (3); print g; What does the program print? Draw a picture of the run-time stack when A has just been called. For each frame, show the static and dynamic links. You do not have to show the storage for any other information. Refer to the run-time stack, briefly explain how function A finds variable g.Solution
1. The output of this program will be- 8 4 2 3
2. First main()-> B(3)-> R(1)-> R(2)-> R(3) -> A(3)
Firstly main is called then In the main nothing happens till 2nd last line i.e. B(3). Only initializations happens. Now function B() is called and value of a=3 which is added with 5 and assigned to x so x=8. After that R(1) is called which makes m=1 and x is printed i.e. 8. Then it is reduced by half and again R(1+1) is called which makes m=2. Till now first call of R(1) has not completed. This R(2) again prints x which is 4 and make it half and calls R(2+1) which makes m=3 and prints x which is 2 and make it half. Now it calls A(3) as m was 3 which assigns g as 3. Once this much is done A(3) ends which ends R(3) which called it. This ends R(2) which called R(2+1) which ends R(1) which and returns to B(3). As B(3) ends print g executes.
3. The above shown calls defines the value of g as g is given value in A() and A() is called with 3 as an argument which is assigned to g.
