Here are nine fun problems. As with the previous assignment, generate a hardcopy pdf to turn in — Wednesday, November 19, for grads and Thursday, November 20 for undergrads.
Readings: [Scott], Chapters 3 and 6. Consider the "Check Your Understanding" questions as part of your reading assignment (that is, solve as many as you can but do not turn in any solutions).
var x = 100;
function set_x(n) {x = n}
function print_x {write_integer(x);}
function first {set_x(1); print_x();}
function second {var x; set_x(2); print_x();}
set_x(0);
first();
print_x();
second();
print_x();
What does this program print if the language uses static
scoping? What does it print with dynamic scoping? Why?
var x = 100;
function set_x(n) {x = n}
function print_x {write_integer(x);}
function foo(S, P, n) {
var x;
if (n == 1 or n == 3) {set_x(n);} else {S(n);}
if (n == 1 or n == 2) {print_x();} else {P();}
}
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. What
does the program print if the language uses shallow
binding? What does it print with deep binding? Why?
(-b + sqrt(4 × a × c)) / (2 × a)
Do you need a special symbol for unary negation?
LOOP
line := ReadLine;
IF AllBlanks(line) THEN EXIT END;
ConsumeLine(line)
END;
Show how you might accomplish the same task using a while or
repeat loop, if midtest loops were not available. (Hint: one
alternative duplicates part of the code;
another introduces a Boolean flag variable.) How do these
alternatives compare to the midtest version?
int first_zero_row = -1; /* none */
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (A[i][j]) goto next;
}
first_zero_row = i;
break;
next: ;
}
The intent of the code is to find the first all-zero row,
if any, of an n × n matrix. Do you find the example
convincing? Is there a good structured alternative in C?
In any language? Undergraduates only:
Give answers in the form of a three page
paper. Include a brief abstract, a good introductory section,
a background section describing views on the goto statement
throughout history, a beefy section analyzing alternatives
to Rubin's problem, and a good concluding section.