Sale!

Assignment 2 – Higher-order Procedures

$30.00

CSE505
Assignment 2 – Higher-order Procedures
(may be done by a team of two students)

Problems 2 and 3 to be assigned later.
1. This question is about static scoping, recursion, and higher-order procedures in the C
programming language. Run the following C program through Gnu C compiler:
#include <stdio.h>
int main() {
int x, y;
void p1(int y, void q(int)) {
void p2(int x) {
x = y + 2;
printf(“%d\n”, x);
q(y);
}
if (x == y)
q(y);
else p1(y+1, p2);
}

Category:

Description

5/5 - (3 votes)

CSE505
Assignment 2 – Higher-order Procedures
(may be done by a team of two students)

Problems 2 and 3 to be assigned later.
1. This question is about static scoping, recursion, and higher-order procedures in the C
programming language. Run the following C program through Gnu C compiler:
#include <stdio.h>
int main() {
int x, y;
void p1(int y, void q(int)) {
void p2(int x) {
x = y + 2;
printf(“%d\n”, x);
q(y);
}
if (x == y)
q(y);
else p1(y+1, p2);
}
void p2(int x) {
x = y + 2;
printf(“%d\n”, x);
}
x = 2; y = 2;
p1(0, p2);
}
a. What is the sequence of values printed?
b. Draw a Scope Diagram at the point when the last value was just printed.
c. Draw a Stack Diagram at the point when the last value was just printed.
Prepare your answers to parts a, b, and c in a file A2_problem1.pdf. Refer to Lecture 10 for
Scope and Stack Diagrams as well as closures. Please note:
1. In the Scope Diagram, for every call on p1, be sure to show the “closure” that is passed to
q in addition to the value bound to y. It is important to correctly nest the frames for
procedure calls.
2. In the Stack Diagram, it suffices to show just the names of the stack frames along with the
static and dynamic links. Internal details of procedures p1 and p2 are not required.
Compiling and running a C program (say, program.c) using the Gnu C compiler under Linux:
a. Compiling program.c: /util/bin/gcc program.c
b. Executing program.c: ./a.out
What to Submit
Prepare a top-level directory named A2_UBITId1_UBITId2 if the assignment is done by two
students (list UBITId’s in alphabetic order); otherwise, name it as A2_UBITId if the assignment is
done solo.
In this directory place the file A2_problem1.pdf as well as the files for problems 2 and 3 (to be
assigned later).
Compress the directory and submit the compressed file using the online submission procedure –
instructions posted at Resources → Assignments → Online_Submission.pdf. Only one
submission per team is required.
End of Assignment 2