Homework Assignment #2: Control, Subprograms, Scoping, ...
(due on 06/05 at the beginning of class)



Problem #1: Iterative Control Structures

Consider the following ALGOL 60-style for statement:
for i := j + 1 step i * j until 3*j do j := j + 1
The semantics should be clear, but you can refer to page 327 of the book for all details. Assume that the initial value of j is 1. List the sequence of values taken by variable i, assuming the following semantics:
a. (10 points) All expressions are evaluated once at the loop entry
b. (10 points) All expressions are evaluated before each iteration
c. (10 points) step expressions are evaluated once at loop entry, and until expressions are evaluated before each iteration
d. (10 points) until expressions are evaluated once at loop entry, and step expressions are evaluated before each iteration, just after the loop counter is incremented
In all cases, when more than one expression is evaluated at the same time, they are evaluated in left-to-right order. Also, the assignment is always done as soon as its RHS (Right-Hand-Side) is evaluated.

Problem #2: Parameter passing

Consider the following program written in C syntax:
void main() {
    int value = 2, list[5] = {1,3,4,7,9};

    swap(value,list[0]);
    printf("%d and [%d,%d,%d,%d,%d]\n",
                value,list[0],list[1],list[2],list[3],list[4]);

    swap(list[0],list[1]);
    printf("%d and [%d,%d,%d,%d,%d]\n",
                value,list[0],list[1],list[2],list[3],list[4]);

    swap(value,list[value]);
    printf("%d and [%d,%d,%d,%d,%d]\n",
                value,list[0],list[1],list[2],list[3],list[4]);

return 0;
}

void swap(int a, int b) {
    int temp;

    temp = a;
    a = b;
    b = temp;
}
For each of the following parameter-passing methods, what is the output of the program:
a. (10 pts)    Passed by value.
b. (10 pts)    Passed by reference.
c. (10 pts)    Passed by name.
d. (10 pts)    Passed by value-result.

Problem #3: Scoping

Consider the following program written in C-like syntax:
int x;

void set_x(int n) {
    x = n;
}

void print_x() {
   printf("%d\n",x);
}

void first() {
   set_x(1);
   print_x();
}

void second() {
   int x;
   set_x(2);
   print_x();
}

int main() {
   set_x(0);
   first();
   print_x();
   second();
   print_x();
}
a. (20 points) What does this program print if the language uses static scoping?
b. (20 points) What does this program print if the language uses dynamic scoping?

Problem #4: Functions as Parameters

Consider the following program written in C-like syntax:
int x;

void set_x(int n) {
    x = n;
}

void print_x() {
   printf("%d\n",x);
}

void foo(void(S*)(int m), void(P*)(void), int n) {
   int x = 0;
   if ((n == 1) || (n == 3))
      set_x(n);
   else
      S(n);

   if ((n == 1) || (n == 2))
      print_x;
   else
      P();
}

int main() {
   set_x(0); foo(set_x,print_x,1); print_x();
   set_x(0); foo(set_x,print_x,2); print_x();
   set_x(0); foo(set_x,print_x,3); print_x();
   set_x(0); foo(set_x,print_x,4); print_x();
}
Assume that the language uses dynamic scoping.
a. (20 points) What does the program print if the language uses shallow binding?
b. (20 points) What does the program print if the language uses deep binding?