CS 217: Programming Languages

Written Assignment #2

Assigned on Friday, 2/20.
Due in class on Friday, 2/27.

Problem A: Do Chapter 5, problem 5. Answer for either C++ or Java. State which language you are addressing.

Problem B: Suppose that the array A is declared in a language that allows user-defined lower and upper bounds as A[1..3][-2..0][3..5].

  1. Write out all of the elements of A in row-major ordering. Here's a start:

    A[1][-2][3]
    A[1][-2][4]
    ...
    

  2. How far down the list from the top is A[1][-2][4]? (Really big hint: the answer is 1.)

  3. How far down the list from the top is A[3][-1][4]?

  4. How far down the list from the top is A[3][-2][3]?

  5. How far down the list from the top is A[i][-2][3] for some arbitary i?

  6. Suppose instead that A had been defined as A[LB1..UB1][-2..0][3..5]. How far down the list from the top is A[i][-2][3]?

  7. Continuing with the above assumptions, how far down the list from the top is A[i][j][3] for some arbitrary j?

  8. Suppose instead that A had been defined as A[LB1..UB1][LB2..UB2][3..5]. How far down the list from the top is A[i][j][3]?

  9. Continuing with the above assumptions, how far down the list from the top is A[i][j][k] for some arbitrary k?

  10. Suppose instead that A had been defined as A[LB1..UB1][LB2..UB2][LB3..UB3]. How far down the list from the top is A[i][j][k]?

  11. Rewrite your answer to the previous questions so that the virtual origin is separated out.

  12. Finally, suppose instead that A had been stored with column-major ordering instead of row-major ordering. How far down the list from the top is A[i][j][k]? Indicate where the virtual origin is in your formula.

Problem C: Should an optimizing compiler for C++/Java be allowed to change the order of subexpressions in a Boolean expression? Why or why not?

Problem D: Consider the following ALGOL 60-style for statement:

for i := j + 1 step i * j until 3 * j do j := j + 1

Assume that the initial value of j is 1. List the sequence of values for the variable i used, assuming the following semantics:

  1. All expressions are evaluated once at the loop entry.

  2. All expressions are evaluated before each iteration. In the for statement, i is updated first, then the step expression, and then the until expression.

  3. step expressions are evaluated once at loop entry, and until expressions are evaluated before each iteration.

  4. until expressions are evaluated once at loop entry, and step expressions are evaluated before each iteration, just after the loop counter is incremented.

Consider the Java/C++ version of the same code:

int j = 1;
for (int i=j+1; i <= 3*j; i+=i*j)
  j++;

Is Java/C++ characterized by any of the above four options, or some other description? Feel free to run experiments with a computer to find out.